ScrollBaseViewController.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. //
  2. // ScrollBaseViewController.m
  3. // CommonLibrary
  4. //
  5. // Created by Alexi on 3/18/14.
  6. // Copyright (c) 2014 Alexi. All rights reserved.
  7. //
  8. #if kSupportScrollController
  9. #import "ScrollBaseViewController.h"
  10. @interface ScrollBaseViewController ()
  11. @end
  12. @implementation ScrollBaseViewController
  13. - (void)viewDidLoad
  14. {
  15. [super viewDidLoad];
  16. _isExpanded = YES;
  17. _handleTabbar = NO;
  18. if (self.tabBarController && !self.tabBarController.tabBar.hidden)
  19. {
  20. _handleTabbar = YES;
  21. }
  22. }
  23. - (void)viewWillAppear:(BOOL)animated
  24. {
  25. [super viewWillAppear:animated];
  26. if (_handleTabbar)
  27. {
  28. self.tabBarController.tabBar.translucent = YES;
  29. }
  30. }
  31. - (void)viewWillDisappear:(BOOL)animated
  32. {
  33. [super viewWillDisappear:animated];
  34. [self backToOrignal];
  35. }
  36. //
  37. //- (void)setBarTintColor
  38. //{
  39. // if ([IOSDeviceConfig sharedConfig].isIOS7Later)
  40. // {
  41. // [self.overlay setBackgroundColor:self.navigationController.navigationBar.barTintColor];
  42. // }
  43. // else
  44. // {
  45. // [self.overlay setBackgroundColor:kNavBarThemeColor];
  46. // }
  47. //}
  48. - (void)followScrollView:(UIView *)scrollableView
  49. {
  50. if (self.scrollableView)
  51. {
  52. [self.scrollableView removeGestureRecognizer:self.panGesture];
  53. }
  54. self.scrollableView = scrollableView;
  55. self.panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
  56. [self.panGesture setMaximumNumberOfTouches:1];
  57. [self.panGesture setDelegate:self];
  58. [self.scrollableView addGestureRecognizer:self.panGesture];
  59. CGRect frame = self.navigationController.navigationBar.frame;
  60. frame.origin = CGPointZero;
  61. // self.overlay = [self crateOverlay:frame];
  62. // [self configOverlay];
  63. }
  64. - (UIView *)crateOverlay:(CGRect)frame
  65. {
  66. return [[UIView alloc] initWithFrame:frame];
  67. }
  68. //- (void)configOverlay
  69. //{
  70. // [self setBarTintColor];
  71. //
  72. //
  73. // [self.overlay setUserInteractionEnabled:NO];
  74. // [self.navigationController.navigationBar addSubview:self.overlay];
  75. // [self.overlay setAlpha:0];
  76. //}
  77. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
  78. {
  79. return YES;
  80. }
  81. #define kNavgationBarOffset 44
  82. #define kStatusHeight 20
  83. - (void)onScrollUp:(CGFloat)delta
  84. {
  85. CGRect frame;
  86. if (self.isCollapsed)
  87. {
  88. return;
  89. }
  90. frame = self.navigationController.navigationBar.frame;
  91. if (frame.origin.y - delta < -kNavgationBarOffset)
  92. {
  93. delta = frame.origin.y + kNavgationBarOffset;
  94. }
  95. frame.origin.y = MAX(-kNavgationBarOffset, frame.origin.y - delta);
  96. self.navigationController.navigationBar.frame = frame;
  97. if (frame.origin.y == -kNavgationBarOffset)
  98. {
  99. self.isCollapsed = YES;
  100. self.isExpanded = NO;
  101. }
  102. [self updateSizingWithDelta:delta];
  103. if ([self.scrollableView isKindOfClass:[UIScrollView class]])
  104. {
  105. [(UIScrollView *)self.scrollableView setContentOffset:CGPointMake(((UIScrollView*)self.scrollableView).contentOffset.x, ((UIScrollView*)self.scrollableView).contentOffset.y - delta)];
  106. }
  107. [self hideTabbar];
  108. [self layoutOnScrollUp];
  109. }
  110. - (void)showTabbar
  111. {
  112. if (_handleTabbar && self.tabBarController.tabBar.translucent)
  113. {
  114. if (self.tabBarController.tabBar.hidden)
  115. {
  116. self.tabBarController.tabBar.hidden = NO;
  117. }
  118. }
  119. }
  120. - (void)onScrollDown:(CGFloat)delta
  121. {
  122. CGRect frame;
  123. if (self.isExpanded)
  124. {
  125. return;
  126. }
  127. frame = self.navigationController.navigationBar.frame;
  128. if (frame.origin.y - delta > kStatusHeight)
  129. {
  130. delta = frame.origin.y - kStatusHeight;
  131. }
  132. frame.origin.y = MIN(kStatusHeight, frame.origin.y - delta);
  133. self.navigationController.navigationBar.frame = frame;
  134. if (frame.origin.y == kStatusHeight)
  135. {
  136. self.isExpanded = YES;
  137. self.isCollapsed = NO;
  138. }
  139. [self updateSizingWithDelta:delta];
  140. [self showTabbar];
  141. [self layoutOnScrollDown];
  142. }
  143. - (void)layoutOnScrollUp
  144. {
  145. }
  146. - (void)layoutOnScrollDown
  147. {
  148. }
  149. - (void)hideTabbar
  150. {
  151. if (_handleTabbar && self.tabBarController.tabBar.translucent)
  152. {
  153. if (!self.tabBarController.tabBar.hidden)
  154. {
  155. self.tabBarController.tabBar.hidden = YES;
  156. }
  157. }
  158. }
  159. #define kScrollHorOffset 20
  160. - (void)handlePan:(UIPanGestureRecognizer *)gesture
  161. {
  162. switch (gesture.state)
  163. {
  164. case UIGestureRecognizerStateChanged:
  165. {
  166. CGPoint translation = [gesture translationInView:[self.scrollableView superview]];
  167. float delta = self.lastContentOffset.y - translation.y;
  168. self.lastContentOffset = translation;
  169. if (delta == 0)
  170. {
  171. return;
  172. }
  173. if (delta > 0)
  174. {
  175. [self onScrollUp:delta];
  176. }
  177. else
  178. {
  179. [self onScrollDown:delta];
  180. }
  181. }
  182. break;
  183. case UIGestureRecognizerStateEnded:
  184. {
  185. self.lastContentOffset = CGPointZero;
  186. [self checkForPartialScroll];
  187. }
  188. break;
  189. default:
  190. break;
  191. }
  192. }
  193. - (void)checkForPartialScrollEnd
  194. {
  195. }
  196. - (void)checkForPartialScroll
  197. {
  198. CGFloat pos = self.navigationController.navigationBar.frame.origin.y;
  199. __block CGFloat delta = 0;
  200. // Get back down
  201. if (pos >= 0)
  202. {
  203. [UIView animateWithDuration:0.2 animations:^{
  204. CGRect frame;
  205. frame = self.navigationController.navigationBar.frame;
  206. delta = frame.origin.y - kStatusHeight;
  207. frame.origin.y = MIN(kStatusHeight, frame.origin.y - delta);
  208. self.navigationController.navigationBar.frame = frame;
  209. self.isExpanded = YES;
  210. self.isCollapsed = NO;
  211. } completion:^(BOOL finished) {
  212. [self updateSizingWithDelta:delta];
  213. [self checkForPartialScrollEnd];
  214. }];
  215. }
  216. else
  217. {
  218. // And back up
  219. [UIView animateWithDuration:0.2 animations:^{
  220. CGRect frame;
  221. frame = self.navigationController.navigationBar.frame;
  222. CGFloat delta = frame.origin.y + kNavgationBarOffset;
  223. frame.origin.y = MAX(-kNavgationBarOffset, frame.origin.y - delta);
  224. self.navigationController.navigationBar.frame = frame;
  225. self.isExpanded = NO;
  226. self.isCollapsed = YES;
  227. } completion:^(BOOL finished) {
  228. [self updateSizingWithDelta:delta];
  229. [self checkForPartialScrollEnd];
  230. }];
  231. }
  232. }
  233. - (void)backToOrignal
  234. {
  235. // 如果navigationbar 或 tab隐藏时进行push,会有界面乱的情况
  236. [self onScrollDown:-kNavgationBarOffset];
  237. self.lastContentOffset = CGPointZero;
  238. [self checkForPartialScroll];
  239. }
  240. - (void)updateSizingWithDelta:(CGFloat)delta
  241. {
  242. DebugLog(@"delta = %f", delta);
  243. CGRect frame = self.navigationController.navigationBar.frame;
  244. float alpha = (frame.origin.y + kNavgationBarOffset) / frame.size.height;
  245. // [self.overlay setAlpha:1 - alpha];
  246. self.navigationController.navigationBar.tintColor = [self.navigationController.navigationBar.tintColor colorWithAlphaComponent:alpha];
  247. UIView *scrollSuperView = self.scrollableView.superview;
  248. frame = scrollSuperView.frame;
  249. frame.origin.y = self.navigationController.navigationBar.frame.origin.y + self.navigationController.navigationBar.frame.size.height;
  250. frame.size.height = frame.size.height + delta;
  251. scrollSuperView.frame = frame;
  252. frame = self.scrollableView.layer.frame;
  253. frame.size.height += delta;
  254. self.scrollableView.layer.frame = frame;
  255. self.scrollableView.frame = frame;
  256. // DebugLog(@"=====>>>>>>>>>>on scroll");
  257. }
  258. @end
  259. #endif