CCControlCenter.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. //
  2. // CCControlCenter.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 07/04/16.
  6. // Copyright (c) 2014 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "CCControlCenter.h"
  24. #import "CCControlCenterTransfer.h"
  25. #import "CCControlCenterActivity.h"
  26. #import "AppDelegate.h"
  27. #import "CCMain.h"
  28. #import "CCDetail.h"
  29. #define BORDER_TOUCH_UPDOWN 50.0f
  30. #define TOOLBAR_ADD_BORDER 20.0f
  31. #define SIZE_FONT_NORECORD 18.0f
  32. #define ANIMATION_GESTURE 0.50f
  33. @interface CCControlCenter ()
  34. {
  35. UIVisualEffectView *_mainView;
  36. UILabel *_noRecord;
  37. UIImageView *_imageDrag;
  38. UIView *_endLine;
  39. CGFloat start, stop;
  40. UIPanGestureRecognizer *panNavigationBar, *panImageDrag;
  41. UITapGestureRecognizer *_singleFingerTap;
  42. UIPageControl *pageControl;
  43. }
  44. @end
  45. @implementation CCControlCenter
  46. #pragma --------------------------------------------------------------------------------------------
  47. #pragma mark ===== Init =====
  48. #pragma --------------------------------------------------------------------------------------------
  49. - (id)initWithCoder:(NSCoder *)aDecoder
  50. {
  51. if (self = [super initWithCoder:aDecoder]) {
  52. app.controlCenter = self;
  53. }
  54. return self;
  55. }
  56. - (void)viewDidLoad
  57. {
  58. [super viewDidLoad];
  59. _controlCenterPagesContent = [NSMutableArray new];
  60. UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
  61. _mainView = [[UIVisualEffectView alloc] initWithEffect:effect];
  62. [_mainView setFrame:CGRectMake(0, 0, self.navigationBar.frame.size.width, 0)];
  63. _mainView.hidden = YES;
  64. // TEST
  65. //_mainView.backgroundColor = [UIColor yellowColor];
  66. // Create data model
  67. _pageType = @[k_pageControlCenterTransfer, k_pageControlCenterActivity];
  68. _currentPageType = k_pageControlCenterTransfer;
  69. // Create page view controller
  70. _pageViewController = [[UIStoryboard storyboardWithName: @"ControlCenter" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"ControlCenterPageViewController"];
  71. _pageViewController.dataSource = self;
  72. _pageViewController.delegate = self;
  73. UIViewController *startingViewController = [self viewControllerAtIndex:0];
  74. NSArray *viewControllers = @[startingViewController];
  75. [_pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
  76. [_pageViewController.view setFrame:CGRectMake(0, 0, self.navigationBar.frame.size.width, 0)];
  77. _pageViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  78. [_mainView addSubview:_pageViewController.view];
  79. _noRecord =[[UILabel alloc]init];
  80. _noRecord.backgroundColor=[UIColor clearColor];
  81. _noRecord.textColor = COLOR_CONTROL_CENTER;
  82. _noRecord.font = [UIFont systemFontOfSize:SIZE_FONT_NORECORD];
  83. _noRecord.textAlignment = NSTextAlignmentCenter;
  84. _noRecord.text = NSLocalizedString(@"_no_transfer_",nil);
  85. _noRecord.hidden = YES;
  86. [_mainView addSubview:_noRecord];
  87. _imageDrag = [[UIImageView alloc] init];
  88. [_imageDrag setFrame:CGRectMake(0, 0, self.navigationBar.frame.size.width, 0)];
  89. _imageDrag.image = [UIImage imageNamed:image_drag];
  90. _imageDrag.contentMode = UIViewContentModeCenter;
  91. _imageDrag.userInteractionEnabled = YES;
  92. [_mainView addSubview:_imageDrag];
  93. _endLine = [[UIView alloc] init];
  94. [_endLine setFrame:CGRectMake(0, 0, self.navigationBar.frame.size.width, 0)];
  95. _endLine.backgroundColor = COLOR_CONTROL_CENTER;
  96. [_mainView addSubview:_endLine];
  97. [self.navigationBar.superview insertSubview:_mainView belowSubview:self.navigationBar];
  98. // Pop Gesture Recognizer
  99. [self.interactivePopGestureRecognizer addTarget:self action:@selector(handlePopGesture:)];
  100. panImageDrag = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
  101. panImageDrag.maximumNumberOfTouches = panImageDrag.minimumNumberOfTouches = 1;
  102. [_imageDrag addGestureRecognizer:panImageDrag];
  103. panNavigationBar = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
  104. panNavigationBar.maximumNumberOfTouches = panNavigationBar.minimumNumberOfTouches = 1;
  105. [self.navigationBar addGestureRecognizer:panNavigationBar];
  106. }
  107. /* iOS Developer Library : A panning gesture is continuous. It begins (UIGestureRecognizerStateBegan) when the minimum number of fingers allowed (minimumNumberOfTouches) has moved enough to be considered a pan. It changes (UIGestureRecognizerStateChanged) when a finger moves while at least the minimum number of fingers are pressed down. It ends (UIGestureRecognizerStateEnded) when all fingers are lifted. */
  108. - (void)handlePanGesture:(UIPanGestureRecognizer *)gesture;
  109. {
  110. CGPoint currentPoint = [gesture locationInView:self.view];
  111. CGFloat navigationBarH = self.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height;
  112. CGFloat navigationBarW = self.navigationBar.frame.size.width;
  113. CGFloat heightScreen = [UIScreen mainScreen].bounds.size.height - self.tabBarController.tabBar.frame.size.height;
  114. CGFloat heightTableView = [UIScreen mainScreen].bounds.size.height - self.tabBarController.tabBar.frame.size.height - self.navigationBar.frame.size.height - BORDER_TOUCH_UPDOWN - TOOLBAR_ADD_BORDER;
  115. float centerMaxH = [self getMaxH] / 2;
  116. float step = [self getMaxH] / 10;
  117. float tableViewY = navigationBarH;
  118. // BUG Scroll to top of UITableView by tapping status bar
  119. _mainView.hidden = NO;
  120. // start
  121. if (gesture.state == UIGestureRecognizerStateBegan) {
  122. start = currentPoint.y;
  123. }
  124. // cancelled (return to 0)
  125. if (gesture.state == UIGestureRecognizerStateCancelled) {
  126. currentPoint.y = 0;
  127. tableViewY = 0;
  128. panNavigationBar.enabled = YES;
  129. }
  130. // end
  131. if (gesture.state == UIGestureRecognizerStateEnded) {
  132. stop = currentPoint.y;
  133. // DOWN
  134. if (start < stop) {
  135. if (stop < (start + step + navigationBarH) && start <= navigationBarH) {
  136. currentPoint.y = 0;
  137. tableViewY = 0;
  138. panNavigationBar.enabled = YES;
  139. } else {
  140. currentPoint.y = [self getMaxH];
  141. tableViewY = navigationBarH;
  142. panNavigationBar.enabled = NO;
  143. }
  144. }
  145. // UP
  146. if (start >= stop && start >= navigationBarH) {
  147. if (stop > (start - step)) {
  148. currentPoint.y = [self getMaxH];
  149. tableViewY = navigationBarH;
  150. panNavigationBar.enabled = NO;
  151. } else {
  152. currentPoint.y = 0;
  153. tableViewY = 0;
  154. panNavigationBar.enabled = YES;
  155. }
  156. }
  157. }
  158. // changed
  159. if (gesture.state == UIGestureRecognizerStateChanged) {
  160. if (currentPoint.y <= navigationBarH) {
  161. currentPoint.y = 0;
  162. tableViewY = 0;
  163. }
  164. else if (currentPoint.y >= [self getMaxH]) {
  165. currentPoint.y = [self getMaxH];
  166. tableViewY = navigationBarH;
  167. }
  168. else {
  169. if (currentPoint.y - navigationBarH < navigationBarH) tableViewY = currentPoint.y - navigationBarH;
  170. else tableViewY = navigationBarH;
  171. }
  172. }
  173. [UIView animateWithDuration:ANIMATION_GESTURE delay:0.0 options:UIViewAnimationOptionTransitionCrossDissolve
  174. animations:^ {
  175. _mainView.frame = CGRectMake(0, 0, navigationBarW, currentPoint.y);
  176. _pageViewController.view.frame = CGRectMake(0, currentPoint.y - heightScreen + navigationBarH, navigationBarW, heightTableView);
  177. _noRecord.frame = CGRectMake(0, currentPoint.y - centerMaxH, navigationBarW, SIZE_FONT_NORECORD+10);
  178. _imageDrag.frame = CGRectMake(0, currentPoint.y - BORDER_TOUCH_UPDOWN, navigationBarW, BORDER_TOUCH_UPDOWN);
  179. _endLine.frame = CGRectMake(0, currentPoint.y - BORDER_TOUCH_UPDOWN, navigationBarW, 1);
  180. } completion:^ (BOOL completed) {
  181. if (_mainView.frame.size.height == [self getMaxH]) {
  182. [self setIsOpen:YES];
  183. } else {
  184. [self setIsOpen:NO];
  185. }
  186. // BUG Scroll to top of UITableView by tapping status bar
  187. if (_mainView.frame.size.height == 0)
  188. _mainView.hidden = YES;
  189. }
  190. ];
  191. }
  192. /* iOS Developer Library : The navigation controller installs this gesture recognizer on its view and uses it to pop the topmost view controller off the navigation stack. You can use this property to retrieve the gesture recognizer and tie it to the behavior of other gesture recognizers in your user interface. When tying your gesture recognizers together, make sure they recognize their gestures simultaneously to ensure that your gesture recognizers are given a chance to handle the event. */
  193. - (void)handlePopGesture:(UIGestureRecognizer *)gesture
  194. {
  195. if (gesture.state == UIGestureRecognizerStateBegan) {
  196. _isPopGesture = YES;
  197. }
  198. if (gesture.state == UIGestureRecognizerStateEnded) {
  199. _isPopGesture = NO;
  200. }
  201. }
  202. // rotazione
  203. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  204. {
  205. if (_isOpen) {
  206. [self setControlCenterHidden:YES];
  207. [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
  208. [self openControlCenterToSize:size];
  209. [self setControlCenterHidden: self.navigationBarHidden];
  210. }];
  211. //[self closeControlCenter];
  212. }
  213. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  214. }
  215. - (void)setControlCenterHidden:(BOOL)hidden
  216. {
  217. _mainView.hidden = hidden;
  218. }
  219. - (float)getMaxH
  220. {
  221. //return ([UIScreen mainScreen].bounds.size.height / 4) * 3;
  222. return [UIScreen mainScreen].bounds.size.height - self.tabBarController.tabBar.frame.size.height;
  223. }
  224. - (void)openControlCenterToSize:(CGSize)size
  225. {
  226. CGFloat navigationBarH = self.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height;
  227. _mainView.frame = CGRectMake(0, 0, size.width, [self getMaxH]);
  228. _pageViewController.view.frame = CGRectMake(0, navigationBarH, _mainView.frame.size.width, _mainView.frame.size.height - self.navigationBar.frame.size.height - BORDER_TOUCH_UPDOWN - TOOLBAR_ADD_BORDER);
  229. _noRecord.frame = CGRectMake(0, _mainView.frame.size.height / 2, _mainView.frame.size.width, SIZE_FONT_NORECORD+10);
  230. _imageDrag.frame = CGRectMake(0, _mainView.frame.size.height - BORDER_TOUCH_UPDOWN, _mainView.frame.size.width, BORDER_TOUCH_UPDOWN);
  231. _endLine.frame = CGRectMake(0, _mainView.frame.size.height - BORDER_TOUCH_UPDOWN, _mainView.frame.size.width, 1);
  232. panNavigationBar.enabled = NO;
  233. [self setIsOpen:YES];
  234. }
  235. /*
  236. - (void)closeControlCenterToSize:(CGSize)size
  237. {
  238. _mainView.frame = CGRectMake(0, 0, size.width, 0);
  239. _tableView.frame = CGRectMake(0, 0, _mainView.frame.size.width, 0);
  240. _noRecord.frame = CGRectMake(0, 0, _mainView.frame.size.width, 0);
  241. _imageDrag.frame = CGRectMake(0, 0, _mainView.frame.size.width, 0);
  242. _endLine.frame = CGRectMake(0, 0, _mainView.frame.size.width, 0);
  243. panNavigationBar.enabled = YES;
  244. }
  245. */
  246. - (void)setIsOpen:(BOOL)setOpen
  247. {
  248. if (setOpen) {
  249. if (!_isOpen) {
  250. _isOpen = YES;
  251. [app.controlCenterTransfer reloadDatasource];
  252. }
  253. } else {
  254. _isOpen = NO;
  255. }
  256. }
  257. #pragma --------------------------------------------------------------------------------------------
  258. #pragma mark - ===== Single Finger Tap =====
  259. #pragma --------------------------------------------------------------------------------------------
  260. - (void)enableSingleFingerTap:(SEL)selector target:(id)target
  261. {
  262. _singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:target action:selector];
  263. [self.navigationBar addGestureRecognizer:_singleFingerTap];
  264. }
  265. - (void)disableSingleFingerTap
  266. {
  267. [self.navigationBar removeGestureRecognizer:_singleFingerTap];
  268. }
  269. #pragma --------------------------------------------------------------------------------------------
  270. #pragma mark ===== Page =====
  271. #pragma --------------------------------------------------------------------------------------------
  272. - (UIViewController *)viewControllerAtIndex:(NSUInteger)index
  273. {
  274. if (([self.pageType count] == 0) || (index >= [self.pageType count])) {
  275. return nil;
  276. }
  277. UIViewController *pageContentViewController;
  278. if ([self.controlCenterPagesContent count] >= index+1) {
  279. pageContentViewController = [self.controlCenterPagesContent objectAtIndex:index];
  280. } else {
  281. if (index == 0)
  282. pageContentViewController = [[UIStoryboard storyboardWithName: @"ControlCenter" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"ControlCenterTransfer"];
  283. if (index == 1)
  284. pageContentViewController = [[UIStoryboard storyboardWithName: @"ControlCenter" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"ControlCenterActivity"];
  285. [self.controlCenterPagesContent addObject:pageContentViewController];
  286. }
  287. ((CCControlCenterTransfer *) pageContentViewController).pageIndex = index;
  288. ((CCControlCenterTransfer *) pageContentViewController).pageType = self.pageType[index];
  289. return pageContentViewController;
  290. }
  291. - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
  292. {
  293. NSUInteger index = ((CCControlCenterTransfer *) viewController).pageIndex;
  294. if ((index == 0) || (index == NSNotFound)) {
  295. return nil;
  296. }
  297. index--;
  298. return [self viewControllerAtIndex:index];
  299. }
  300. - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
  301. {
  302. NSUInteger index = ((CCControlCenterTransfer *) viewController).pageIndex;
  303. if (index == NSNotFound) {
  304. return nil;
  305. }
  306. index++;
  307. if (index == [self.pageType count]) {
  308. return nil;
  309. }
  310. return [self viewControllerAtIndex:index];
  311. }
  312. - (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
  313. {
  314. return [self.pageType count];
  315. }
  316. - (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
  317. {
  318. return 0;
  319. }
  320. /*
  321. - (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers
  322. {
  323. }
  324. */
  325. - (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed
  326. {
  327. }
  328. @end