PageView.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 "PageView.h"
  21. #import "RectUtil.h"
  22. @interface PageView ()
  23. @property PagingDirection direction;
  24. @property UIPanGestureRecognizer *panGesture;
  25. @end
  26. @implementation PageView
  27. - (id) initWithFrame:(CGRect)frame
  28. {
  29. self = [super initWithFrame:frame];
  30. if (self) {
  31. // clip if not on entire screen
  32. if (self.superview != nil && self.superview.frame.size.width > self.frame.size.width) {
  33. self.clipsToBounds = YES;
  34. }
  35. _panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
  36. [self addGestureRecognizer:_panGesture];
  37. _pageGap = DEFAULT_PAGE_GAP;
  38. _parallaxFactor = DEFAULT_PARALLAX_FACTOR;
  39. }
  40. return self;
  41. }
  42. - (void)setBgView:(UIView *)bgView {
  43. _bgView = bgView;
  44. [self addSubview:_bgView];
  45. [self sendSubviewToBack:_bgView];
  46. }
  47. - (void) setHidden: (BOOL)hidden
  48. {
  49. [super setHidden: hidden];
  50. _centerView.hidden = hidden;
  51. _rightView.hidden = hidden;
  52. _leftView.hidden = hidden;
  53. }
  54. - (void) setDatasource:(id<PageViewDataSource>)datasource
  55. {
  56. _datasource = datasource;
  57. [self reset];
  58. }
  59. - (void) resetPageFrames
  60. {
  61. self.leftView.frame = self.bounds;
  62. self.centerView.frame = self.bounds;
  63. self.rightView.frame = self.bounds;
  64. BOOL animationsEnabled = [UIView areAnimationsEnabled];
  65. [UIView setAnimationsEnabled:NO];
  66. [self positionViews];
  67. [UIView setAnimationsEnabled:animationsEnabled];
  68. }
  69. - (void) reset
  70. {
  71. [self preparePagedViews];
  72. [self positionViews];
  73. }
  74. - (void) pageLeft
  75. {
  76. BOOL changePage = [self preparePageLeft];
  77. [self positionViewsWithDuration: MAX_ANIMATION_DURATION changePage:changePage];
  78. }
  79. - (void) pageRight
  80. {
  81. BOOL changePage = [self preparePageRight];
  82. [self positionViewsWithDuration: MAX_ANIMATION_DURATION changePage:changePage];
  83. }
  84. - (void)enablePanGesture:(BOOL)enablePan {
  85. _panGesture.enabled = enablePan;
  86. }
  87. #pragma mark - UIPanGestureRecognizer
  88. - (void) pan: (UIPanGestureRecognizer *) recognizer
  89. {
  90. if (recognizer.state == UIGestureRecognizerStateBegan) {
  91. } else if (recognizer.state == UIGestureRecognizerStateChanged) {
  92. CGPoint transformed = [recognizer translationInView:self];
  93. [self moveAllViewsByX:transformed.x];
  94. [recognizer setTranslation:CGPointZero inView:self];
  95. } else if (recognizer.state == UIGestureRecognizerStateEnded) {
  96. BOOL changePage = NO;
  97. if ([self movedLeftAcrossTrigger] && [self checkSpeedTrigger:recognizer]) {
  98. changePage = [self preparePageLeft];
  99. } else if ([self movedRightAcrossTrigger] && [self checkSpeedTrigger:recognizer]) {
  100. changePage = [self preparePageRight];
  101. }
  102. CGPoint velocity = [recognizer velocityInView:self];
  103. [self positionViewsWithVelocity: velocity changePage:changePage];
  104. }
  105. }
  106. - (BOOL) checkSpeedTrigger: (UIPanGestureRecognizer *) recognizer
  107. {
  108. CGPoint velocity = [recognizer velocityInView:self];
  109. return (fabs(velocity.x) > MIN_PAGE_PAN_SPEED);
  110. }
  111. - (BOOL) movedRightAcrossTrigger
  112. {
  113. return (self.centerView.frame.origin.x ) > MIN_PAGE_PAN_POINTS;
  114. }
  115. - (BOOL) movedLeftAcrossTrigger
  116. {
  117. return (self.centerView.frame.origin.x ) < -MIN_PAGE_PAN_POINTS;
  118. }
  119. - (void) moveAllViewsByX: (CGFloat) x
  120. {
  121. [self moveView:self.leftView X:x Y:0];
  122. [self moveView:self.centerView X:x Y:0];
  123. [self moveView:self.rightView X:x Y:0];
  124. [self moveView:_bgView X:x*_parallaxFactor Y:0.0];
  125. }
  126. - (void) moveView: (UIView *) view X: (CGFloat) x Y: (CGFloat) y
  127. {
  128. CGRect moved = view.frame;
  129. moved.origin.x += x;
  130. view.frame = moved;
  131. }
  132. - (void) placeView: (UIView *) view X: (CGFloat) x Y: (CGFloat) y
  133. {
  134. CGRect newPos = view.frame;
  135. newPos.origin.x = x;
  136. newPos.origin.y = y;
  137. view.frame = newPos;
  138. }
  139. - (BOOL) preparePageRight
  140. {
  141. BOOL canPage = [self.datasource moveToPrevious];
  142. if (canPage == NO) {
  143. return NO;
  144. }
  145. [self.rightView removeFromSuperview];
  146. self.rightView = self.centerView;
  147. self.centerView = self.leftView;
  148. self.leftView = [self.datasource previousView: self.bounds];
  149. [self addSubview:self.leftView];
  150. [self placeView:self.leftView X:-self.frame.size.width Y:self.leftView.frame.origin.y];
  151. _direction = RIGHT;
  152. if ([_delegate respondsToSelector: @selector(willPageFrom:toView:)]) {
  153. [_delegate willPageFrom:self.rightView toView:self.centerView];
  154. }
  155. return YES;
  156. }
  157. - (BOOL) preparePageLeft
  158. {
  159. BOOL canPage = [self.datasource moveToNext];
  160. if (canPage == NO) {
  161. return NO;
  162. }
  163. [self.leftView removeFromSuperview];
  164. self.leftView = self.centerView;
  165. self.centerView = self.rightView;
  166. self.rightView = [self.datasource nextView: self.bounds];
  167. [self addSubview:self.rightView];
  168. [self placeView:self.rightView X:self.frame.size.width Y:self.rightView.frame.origin.y];
  169. _direction = LEFT;
  170. if ([_delegate respondsToSelector: @selector(willPageFrom:toView:)]) {
  171. [_delegate willPageFrom:self.leftView toView:self.centerView];
  172. }
  173. return YES;
  174. }
  175. - (void) positionViews
  176. {
  177. CGFloat currentCenterX = self.centerView.frame.origin.x;
  178. [self placeView:self.leftView X:-(self.frame.size.width + _pageGap) Y:self.leftView.frame.origin.y];
  179. [self placeView:self.centerView X:0.0 Y:self.centerView.frame.origin.y];
  180. [self placeView:self.rightView X:(self.frame.size.width + _pageGap) Y:self.rightView.frame.origin.y];
  181. CGFloat diff = self.centerView.frame.origin.x - currentCenterX;
  182. CGFloat newBgXOffset = self.bgView.frame.origin.x + diff * _parallaxFactor;
  183. [self placeView:self.bgView X:newBgXOffset Y:self.bgView.frame.origin.y];
  184. }
  185. - (void) positionViewsWithVelocity: (CGPoint) velocity changePage:(BOOL)changePage
  186. {
  187. CGFloat points2move = fabs(self.centerView.frame.origin.x);
  188. CGFloat duration = points2move / fabs(velocity.x);
  189. duration = MIN(duration, MAX_ANIMATION_DURATION);
  190. [self positionViewsWithDuration: duration changePage:changePage];
  191. }
  192. - (void) positionViewsWithDuration: (CGFloat) duration changePage:(BOOL)changePage
  193. {
  194. UIViewAnimationOptions options = UIViewAnimationCurveEaseOut | UIViewAnimationOptionBeginFromCurrentState;
  195. [UIView animateWithDuration:duration delay:0.0 options:options animations:^{
  196. [self positionViews];
  197. } completion:^(BOOL finished) {
  198. if (changePage) {
  199. [self notifyDelegate];
  200. }
  201. }];
  202. }
  203. - (void) preparePagedViews
  204. {
  205. for (UIView *view in [self subviews]) {
  206. [view removeFromSuperview];
  207. }
  208. self.centerView = [self.datasource currentView: self.bounds];
  209. [self addSubview:self.centerView];
  210. self.leftView = [self.datasource previousView: self.bounds];
  211. [self addSubview:self.leftView];
  212. self.rightView = [self.datasource nextView: self.bounds];
  213. [self addSubview:self.rightView];
  214. }
  215. - (void) notifyDelegate
  216. {
  217. if ([_delegate respondsToSelector: @selector(didPageFrom:toView:)]) {
  218. if (_direction == RIGHT) {
  219. [_delegate didPageFrom:self.rightView toView:self.centerView];
  220. } else if (_direction == LEFT) {
  221. [_delegate didPageFrom:self.leftView toView:self.centerView];
  222. }
  223. }
  224. }
  225. - (CGSize) sizeThatFits:(CGSize)size
  226. {
  227. return [self.centerView sizeThatFits:size];
  228. }
  229. @end