CCControlCenter.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 "CCControlCenterPageContent.h"
  25. #import "AppDelegate.h"
  26. #import "CCMain.h"
  27. #import "CCDetail.h"
  28. #define BORDER_TOUCH_UPDOWN 50.0f
  29. #define TOOLBAR_TRANSFER_H 0.0f
  30. #define TOOLBAR_ADD_BORDER 20.0f
  31. #define SIZE_FONT_NORECORD 18.0f
  32. #define ANIMATION_GESTURE 0.50f
  33. #define download 1
  34. #define downloadwwan 2
  35. #define upload 3
  36. #define uploadwwan 4
  37. @interface CCControlCenter ()
  38. {
  39. UIVisualEffectView *_mainView;
  40. UILabel *_noRecord;
  41. UIImageView *_imageDrag;
  42. UIView *_endLine;
  43. CGFloat start, stop;
  44. UIPanGestureRecognizer *panNavigationBar, *panImageDrag;
  45. UITapGestureRecognizer *_singleFingerTap;
  46. UIPageControl *pageControl;
  47. }
  48. @end
  49. @implementation CCControlCenter
  50. #pragma --------------------------------------------------------------------------------------------
  51. #pragma mark ===== Init =====
  52. #pragma --------------------------------------------------------------------------------------------
  53. - (id)initWithCoder:(NSCoder *)aDecoder
  54. {
  55. if (self = [super initWithCoder:aDecoder]) {
  56. }
  57. return self;
  58. }
  59. - (void)viewDidLoad
  60. {
  61. [super viewDidLoad];
  62. _controlCenterPagesContent = [NSMutableArray new];
  63. UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
  64. _mainView = [[UIVisualEffectView alloc] initWithEffect:effect];
  65. [_mainView setFrame:CGRectMake(0, 0, self.navigationBar.frame.size.width, 0)];
  66. _mainView.hidden = YES;
  67. // TEST
  68. //_mainView.backgroundColor = [UIColor yellowColor];
  69. // Create data model
  70. _pageType = @[k_pageControlCenterTransfer, k_pageControlCenterActivity];
  71. _currentPageType = k_pageControlCenterTransfer;
  72. // Create page view controller
  73. _pageViewController = [[UIStoryboard storyboardWithName: @"ControlCenter" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"ControlCenterPageViewController"];
  74. _pageViewController.dataSource = self;
  75. _pageViewController.delegate = self;
  76. CCControlCenterPageContent *startingViewController = [self viewControllerAtIndex:0];
  77. NSArray *viewControllers = @[startingViewController];
  78. [_pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
  79. [_pageViewController.view setFrame:CGRectMake(0, 0, self.navigationBar.frame.size.width, 0)];
  80. _pageViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  81. [_mainView addSubview:_pageViewController.view];
  82. _noRecord =[[UILabel alloc]init];
  83. _noRecord.backgroundColor=[UIColor clearColor];
  84. _noRecord.textColor = COLOR_CONTROL_CENTER;
  85. _noRecord.font = [UIFont systemFontOfSize:SIZE_FONT_NORECORD];
  86. _noRecord.textAlignment = NSTextAlignmentCenter;
  87. _noRecord.text = NSLocalizedString(@"_no_transfer_",nil);
  88. _noRecord.hidden = YES;
  89. [_mainView addSubview:_noRecord];
  90. _imageDrag = [[UIImageView alloc] init];
  91. [_imageDrag setFrame:CGRectMake(0, 0, self.navigationBar.frame.size.width, 0)];
  92. _imageDrag.image = [UIImage imageNamed:image_drag];
  93. _imageDrag.contentMode = UIViewContentModeCenter;
  94. _imageDrag.userInteractionEnabled = YES;
  95. [_mainView addSubview:_imageDrag];
  96. _endLine = [[UIView alloc] init];
  97. [_endLine setFrame:CGRectMake(0, 0, self.navigationBar.frame.size.width, 0)];
  98. _endLine.backgroundColor = COLOR_CONTROL_CENTER;
  99. [_mainView addSubview:_endLine];
  100. [self.navigationBar.superview insertSubview:_mainView belowSubview:self.navigationBar];
  101. // Pop Gesture Recognizer
  102. [self.interactivePopGestureRecognizer addTarget:self action:@selector(handlePopGesture:)];
  103. panImageDrag = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
  104. panImageDrag.maximumNumberOfTouches = panImageDrag.minimumNumberOfTouches = 1;
  105. [_imageDrag addGestureRecognizer:panImageDrag];
  106. panNavigationBar = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
  107. panNavigationBar.maximumNumberOfTouches = panNavigationBar.minimumNumberOfTouches = 1;
  108. [self.navigationBar addGestureRecognizer:panNavigationBar];
  109. }
  110. /* 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. */
  111. - (void)handlePanGesture:(UIPanGestureRecognizer *)gesture;
  112. {
  113. CGPoint currentPoint = [gesture locationInView:self.view];
  114. CGFloat navigationBarH = self.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height;
  115. CGFloat navigationBarW = self.navigationBar.frame.size.width;
  116. CGFloat heightScreen = [UIScreen mainScreen].bounds.size.height - self.tabBarController.tabBar.frame.size.height;
  117. CGFloat heightTableView = [UIScreen mainScreen].bounds.size.height - self.tabBarController.tabBar.frame.size.height - navigationBarH - BORDER_TOUCH_UPDOWN - TOOLBAR_TRANSFER_H - TOOLBAR_ADD_BORDER;
  118. float centerMaxH = [self getMaxH] / 2;
  119. float step = [self getMaxH] / 10;
  120. float tableViewY = navigationBarH;
  121. // BUG Scroll to top of UITableView by tapping status bar
  122. _mainView.hidden = NO;
  123. // start
  124. if (gesture.state == UIGestureRecognizerStateBegan) {
  125. start = currentPoint.y;
  126. }
  127. // cancelled (return to 0)
  128. if (gesture.state == UIGestureRecognizerStateCancelled) {
  129. currentPoint.y = 0;
  130. tableViewY = 0;
  131. panNavigationBar.enabled = YES;
  132. }
  133. // end
  134. if (gesture.state == UIGestureRecognizerStateEnded) {
  135. stop = currentPoint.y;
  136. // DOWN
  137. if (start < stop) {
  138. if (stop < (start + step + navigationBarH) && start <= navigationBarH) {
  139. currentPoint.y = 0;
  140. tableViewY = 0;
  141. panNavigationBar.enabled = YES;
  142. } else {
  143. currentPoint.y = [self getMaxH];
  144. tableViewY = navigationBarH;
  145. panNavigationBar.enabled = NO;
  146. }
  147. }
  148. // UP
  149. if (start >= stop && start >= navigationBarH) {
  150. if (stop > (start - step)) {
  151. currentPoint.y = [self getMaxH];
  152. tableViewY = navigationBarH;
  153. panNavigationBar.enabled = NO;
  154. } else {
  155. currentPoint.y = 0;
  156. tableViewY = 0;
  157. panNavigationBar.enabled = YES;
  158. }
  159. }
  160. }
  161. // changed
  162. if (gesture.state == UIGestureRecognizerStateChanged) {
  163. if (currentPoint.y <= navigationBarH) {
  164. currentPoint.y = 0;
  165. tableViewY = 0;
  166. }
  167. else if (currentPoint.y >= [self getMaxH]) {
  168. currentPoint.y = [self getMaxH];
  169. tableViewY = navigationBarH;
  170. }
  171. else {
  172. if (currentPoint.y - navigationBarH < navigationBarH) tableViewY = currentPoint.y - navigationBarH;
  173. else tableViewY = navigationBarH;
  174. }
  175. }
  176. [UIView animateWithDuration:ANIMATION_GESTURE delay:0.0 options:UIViewAnimationOptionTransitionCrossDissolve
  177. animations:^ {
  178. _mainView.frame = CGRectMake(0, 0, navigationBarW, currentPoint.y);
  179. _pageViewController.view.frame = CGRectMake(0, currentPoint.y - heightScreen + navigationBarH, navigationBarW, heightTableView);
  180. _noRecord.frame = CGRectMake(0, currentPoint.y - centerMaxH - TOOLBAR_TRANSFER_H, navigationBarW, SIZE_FONT_NORECORD+10);
  181. _imageDrag.frame = CGRectMake(0, currentPoint.y - BORDER_TOUCH_UPDOWN, navigationBarW, BORDER_TOUCH_UPDOWN);
  182. _endLine.frame = CGRectMake(0, currentPoint.y - BORDER_TOUCH_UPDOWN, navigationBarW, 1);
  183. } completion:^ (BOOL completed) {
  184. if (_mainView.frame.size.height == [self getMaxH]) {
  185. [self setIsOpen:YES];
  186. } else {
  187. [self setIsOpen:NO];
  188. }
  189. // BUG Scroll to top of UITableView by tapping status bar
  190. if (_mainView.frame.size.height == 0)
  191. _mainView.hidden = YES;
  192. }
  193. ];
  194. }
  195. /* 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. */
  196. - (void)handlePopGesture:(UIGestureRecognizer *)gesture
  197. {
  198. if (gesture.state == UIGestureRecognizerStateBegan) {
  199. _isPopGesture = YES;
  200. }
  201. if (gesture.state == UIGestureRecognizerStateEnded) {
  202. _isPopGesture = NO;
  203. }
  204. }
  205. // rotazione
  206. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  207. {
  208. if (_isOpen) {
  209. [self setControlCenterHidden:YES];
  210. [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
  211. [self openControlCenterToSize:size];
  212. [self setControlCenterHidden: self.navigationBarHidden];
  213. }];
  214. //[self closeControlCenter];
  215. }
  216. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  217. }
  218. - (void)setControlCenterHidden:(BOOL)hidden
  219. {
  220. _mainView.hidden = hidden;
  221. }
  222. - (float)getMaxH
  223. {
  224. //return ([UIScreen mainScreen].bounds.size.height / 4) * 3;
  225. return [UIScreen mainScreen].bounds.size.height - self.tabBarController.tabBar.frame.size.height;
  226. }
  227. - (void)openControlCenterToSize:(CGSize)size
  228. {
  229. CGFloat navigationBarH = self.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height;
  230. _mainView.frame = CGRectMake(0, 0, size.width, [self getMaxH]);
  231. _pageViewController.view.frame = CGRectMake(0, navigationBarH, _mainView.frame.size.width, _mainView.frame.size.height - navigationBarH - BORDER_TOUCH_UPDOWN - TOOLBAR_TRANSFER_H - TOOLBAR_ADD_BORDER);
  232. _noRecord.frame = CGRectMake(0, _mainView.frame.size.height / 2 - TOOLBAR_TRANSFER_H, _mainView.frame.size.width, SIZE_FONT_NORECORD+10);
  233. _imageDrag.frame = CGRectMake(0, _mainView.frame.size.height - BORDER_TOUCH_UPDOWN, _mainView.frame.size.width, BORDER_TOUCH_UPDOWN);
  234. _endLine.frame = CGRectMake(0, _mainView.frame.size.height - BORDER_TOUCH_UPDOWN, _mainView.frame.size.width, 1);
  235. panNavigationBar.enabled = NO;
  236. [self setIsOpen:YES];
  237. }
  238. /*
  239. - (void)closeControlCenterToSize:(CGSize)size
  240. {
  241. _mainView.frame = CGRectMake(0, 0, size.width, 0);
  242. _tableView.frame = CGRectMake(0, 0, _mainView.frame.size.width, 0);
  243. _noRecord.frame = CGRectMake(0, 0, _mainView.frame.size.width, 0);
  244. _imageDrag.frame = CGRectMake(0, 0, _mainView.frame.size.width, 0);
  245. _endLine.frame = CGRectMake(0, 0, _mainView.frame.size.width, 0);
  246. panNavigationBar.enabled = YES;
  247. }
  248. */
  249. - (void)setIsOpen:(BOOL)setOpen
  250. {
  251. if (setOpen) {
  252. if (!_isOpen) {
  253. _isOpen = YES;
  254. //[self reloadDatasource];
  255. }
  256. } else {
  257. _isOpen = NO;
  258. }
  259. }
  260. #pragma --------------------------------------------------------------------------------------------
  261. #pragma mark - ===== Single Finger Tap =====
  262. #pragma --------------------------------------------------------------------------------------------
  263. - (void)enableSingleFingerTap:(SEL)selector target:(id)target
  264. {
  265. _singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:target action:selector];
  266. [self.navigationBar addGestureRecognizer:_singleFingerTap];
  267. }
  268. - (void)disableSingleFingerTap
  269. {
  270. [self.navigationBar removeGestureRecognizer:_singleFingerTap];
  271. }
  272. #pragma --------------------------------------------------------------------------------------------
  273. #pragma mark ===== Page =====
  274. #pragma --------------------------------------------------------------------------------------------
  275. - (CCControlCenterPageContent *)viewControllerAtIndex:(NSUInteger)index
  276. {
  277. if (([self.pageType count] == 0) || (index >= [self.pageType count])) {
  278. return nil;
  279. }
  280. CCControlCenterPageContent *pageContentViewController;
  281. if ([self.controlCenterPagesContent count] >= index+1) {
  282. pageContentViewController = [self.controlCenterPagesContent objectAtIndex:index];
  283. } else {
  284. // Create a new view controller and pass suitable data.
  285. pageContentViewController = [[UIStoryboard storyboardWithName: @"ControlCenter" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"ControlCenterPageContent"];
  286. [self.controlCenterPagesContent addObject:pageContentViewController];
  287. }
  288. pageContentViewController.pageIndex = index;
  289. pageContentViewController.pageType = self.pageType[index];
  290. return pageContentViewController;
  291. }
  292. - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
  293. {
  294. NSUInteger index = ((CCControlCenterPageContent *) viewController).pageIndex;
  295. if ((index == 0) || (index == NSNotFound)) {
  296. return nil;
  297. }
  298. index--;
  299. return [self viewControllerAtIndex:index];
  300. }
  301. - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
  302. {
  303. NSUInteger index = ((CCControlCenterPageContent *) viewController).pageIndex;
  304. if (index == NSNotFound) {
  305. return nil;
  306. }
  307. index++;
  308. if (index == [self.pageType count]) {
  309. return nil;
  310. }
  311. return [self viewControllerAtIndex:index];
  312. }
  313. - (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
  314. {
  315. return [self.pageType count];
  316. }
  317. - (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
  318. {
  319. return 0;
  320. }
  321. /*
  322. - (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers
  323. {
  324. }
  325. */
  326. - (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed
  327. {
  328. }
  329. @end