ParallaxPageViewController.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2015-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "ParallaxPageViewController.h"
  21. #import "PageContentViewController.h"
  22. #import "PageView.h"
  23. #import "RectUtil.h"
  24. #import "UIImage+ColoredImage.h"
  25. #import "AppDelegate.h"
  26. @interface ParallaxPageViewController () <PageViewDataSource, PageViewDelegate, PageContentControllerDelegate>
  27. @property PageView *pageContainerView;
  28. @property NSInteger index;
  29. @end
  30. @implementation ParallaxPageViewController
  31. @synthesize parallaxFactor = _parallaxFactor;
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. [self setup];
  35. [self updateNavigationButtons];
  36. [self updatePageControl];
  37. _pageContainerView.backgroundColor = [UIColor clearColor];
  38. [self setNeedsStatusBarAppearanceUpdate];
  39. }
  40. - (BOOL)prefersStatusBarHidden {
  41. return YES;
  42. }
  43. - (BOOL)shouldAutorotate {
  44. return NO;
  45. }
  46. -(UIInterfaceOrientationMask)supportedInterfaceOrientations {
  47. if (SYSTEM_IS_IPAD) {
  48. return UIInterfaceOrientationMaskAll;
  49. } else {
  50. return UIInterfaceOrientationMaskPortrait;
  51. }
  52. }
  53. - (void)viewWillAppear:(BOOL)animated {
  54. [super viewWillAppear:animated];
  55. _pageContainerView.bgView = _bgView;
  56. }
  57. - (void)setViewControllers:(NSArray *)viewControllers {
  58. _viewControllers = viewControllers;
  59. // setup parent child relationship
  60. for (PageContentViewController *vc in _viewControllers) {
  61. vc.containerDelegate = self;
  62. [self addChildViewController:vc];
  63. [vc didMoveToParentViewController:self];
  64. }
  65. }
  66. - (void)setup {
  67. _pageContainerView = [[PageView alloc] initWithFrame:self.view.frame];
  68. _pageContainerView.delegate = self;
  69. _pageContainerView.datasource = self;
  70. if (_parallaxFactor) {
  71. _pageContainerView.parallaxFactor = _parallaxFactor.floatValue;
  72. }
  73. [self.view addSubview:_pageContainerView];
  74. [self.view bringSubviewToFront:_controlsView];
  75. [_pageLeftButton setImage:[UIImage imageNamed:@"ArrowPrevious" inColor:[Colors mainThemeDark]] forState:UIControlStateNormal];
  76. [_pageRightButton setImage:[UIImage imageNamed:@"ArrowNext" inColor:[Colors mainThemeDark]] forState:UIControlStateNormal];
  77. _pageLeftButton.accessibilityLabel = [BundleUtil localizedStringForKey:@"previous"];
  78. _pageRightButton.accessibilityLabel = [BundleUtil localizedStringForKey:@"next"];
  79. _pageControl.accessibilityLabel = [NSString stringWithFormat:@"%li %@ %li", (long)_pageControl.currentPage + 1, [BundleUtil localizedStringForKey:@"from"], (long)_pageControl.numberOfPages];
  80. _index = 0;
  81. if ([AppDelegate hasBottomSafeAreaInsets]) {
  82. _controlsView.frame = CGRectMake(_controlsView.frame.origin.x, _controlsView.frame.origin.y - 20.0, _controlsView.frame.size.width, _controlsView.frame.size.height + 20.0);
  83. }
  84. }
  85. - (void)setParallaxFactor:(NSNumber *)parallaxFactor {
  86. _parallaxFactor = parallaxFactor;
  87. _pageContainerView.parallaxFactor = parallaxFactor.floatValue;
  88. }
  89. - (NSNumber *)parallaxFactor {
  90. return [NSNumber numberWithFloat:_pageContainerView.parallaxFactor];
  91. }
  92. - (NSInteger)nextIndexForIncrement:(NSInteger)increment {
  93. return _index + increment;
  94. }
  95. - (UIView *)viewAtIndex:(NSInteger)index {
  96. UIViewController *viewController = (UIViewController *)_viewControllers[index];
  97. return viewController.view;
  98. }
  99. - (void)updateNavigationButtons {
  100. if (_index == 0) {
  101. _pageLeftButton.hidden = YES;
  102. } else {
  103. _pageLeftButton.hidden = NO;
  104. }
  105. if (_index == _viewControllers.count - 1) {
  106. _pageRightButton.hidden = YES;
  107. } else {
  108. _pageRightButton.hidden = NO;
  109. }
  110. }
  111. - (void)updatePageControl {
  112. _pageControl.currentPage = _index;
  113. _pageControl.numberOfPages = _viewControllers.count;
  114. }
  115. #pragma mark - PageViewDataSource
  116. - (UIView *) currentView: (CGRect) frame {
  117. return [self viewAtIndex:_index];
  118. }
  119. - (UIView *) nextView: (CGRect) frame {
  120. if (_index == _viewControllers.count - 1) {
  121. return nil;
  122. }
  123. NSInteger nextIndex = [self nextIndexForIncrement:1];
  124. return [self viewAtIndex:nextIndex];
  125. }
  126. - (UIView *) previousView: (CGRect) frame {
  127. if (_index == 0) {
  128. return nil;
  129. }
  130. NSInteger previousIndex = [self nextIndexForIncrement:-1];
  131. return [self viewAtIndex:previousIndex];
  132. }
  133. - (BOOL) moveToNext {
  134. if (_index == _viewControllers.count - 1) {
  135. return NO;
  136. }
  137. if ([self canMoveFromCurrentViewController]) {
  138. return NO;
  139. }
  140. _index++;
  141. _pageControl.accessibilityLabel = [NSString stringWithFormat:@"%li %@ %li", _index + 1, [BundleUtil localizedStringForKey:@"from"], (long)_pageControl.numberOfPages];
  142. return YES;
  143. }
  144. - (BOOL) moveToPrevious {
  145. if (_index == 0) {
  146. return NO;
  147. }
  148. _index--;
  149. _pageControl.accessibilityLabel = [NSString stringWithFormat:@"%li %@ %li", _index + 1, [BundleUtil localizedStringForKey:@"from"], (long)_pageControl.numberOfPages];
  150. return YES;
  151. }
  152. - (BOOL)canMoveFromCurrentViewController {
  153. PageContentViewController *currentVc = _viewControllers[_index];
  154. if ([currentVc respondsToSelector:@selector(isInputValid)]) {
  155. return [currentVc isInputValid] == NO;
  156. } else {
  157. return YES;
  158. }
  159. }
  160. - (UIViewController *)viewControllerForView:(UIView *)view {
  161. for (UIViewController *vc in _viewControllers) {
  162. if (vc.view == view) {
  163. return vc;
  164. }
  165. }
  166. return nil;
  167. }
  168. #pragma mark - PageViewDelegate
  169. - (void) willPageFrom: (UIView *) fromView toView: (UIView *) toView {
  170. UIViewController *toVc = [self viewControllerForView:toView];
  171. [toVc beginAppearanceTransition:YES animated:YES];
  172. UIViewController *fromVc = [self viewControllerForView:fromView];
  173. [fromVc beginAppearanceTransition:NO animated:YES];
  174. }
  175. - (void) didPageFrom: (UIView *) fromView toView: (UIView *) toView {
  176. UIViewController *toVc = [self viewControllerForView:toView];
  177. [toVc endAppearanceTransition];
  178. UIViewController *fromVc = [self viewControllerForView:fromView];
  179. [fromVc endAppearanceTransition];
  180. [self updateNavigationButtons];
  181. [self updatePageControl];
  182. }
  183. #pragma mark - actions
  184. - (IBAction)pageLeftAction:(id)sender {
  185. [_pageContainerView pageLeft];
  186. }
  187. - (IBAction)pageRightAction:(id)sender {
  188. [_pageContainerView pageRight];
  189. }
  190. - (IBAction)pageControlChanged:(id)sender {
  191. UIPageControl *pc = (UIPageControl *)sender;
  192. if (_index < pc.currentPage) {
  193. [_pageContainerView pageLeft];
  194. } else {
  195. [_pageContainerView pageRight];
  196. }
  197. }
  198. #pragma mark - PageContentControllerDelegate
  199. - (void)hideControls:(BOOL)hideControls {
  200. CGFloat yOffset;
  201. if (hideControls) {
  202. yOffset = CGRectGetMaxY(self.view.frame);
  203. } else {
  204. yOffset = CGRectGetMaxY(self.view.frame) - CGRectGetHeight(_controlsView.frame);
  205. }
  206. CGRect rect = [RectUtil setYPositionOf:_controlsView.frame y:yOffset];
  207. _controlsView.frame = rect;
  208. [_pageContainerView enablePanGesture:(hideControls == NO)];
  209. }
  210. - (void)pageRight {
  211. [_pageContainerView pageRight];
  212. }
  213. - (void)pageLeft {
  214. [_pageContainerView pageLeft];
  215. }
  216. @end