| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- //
- // ScrollBaseViewController.m
- // CommonLibrary
- //
- // Created by Alexi on 3/18/14.
- // Copyright (c) 2014 Alexi. All rights reserved.
- //
- #if kSupportScrollController
- #import "ScrollBaseViewController.h"
- @interface ScrollBaseViewController ()
- @end
- @implementation ScrollBaseViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- _isExpanded = YES;
-
- _handleTabbar = NO;
- if (self.tabBarController && !self.tabBarController.tabBar.hidden)
- {
- _handleTabbar = YES;
- }
- }
- - (void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
-
- if (_handleTabbar)
- {
- self.tabBarController.tabBar.translucent = YES;
- }
- }
- - (void)viewWillDisappear:(BOOL)animated
- {
- [super viewWillDisappear:animated];
- [self backToOrignal];
- }
- //
- //- (void)setBarTintColor
- //{
- // if ([IOSDeviceConfig sharedConfig].isIOS7Later)
- // {
- // [self.overlay setBackgroundColor:self.navigationController.navigationBar.barTintColor];
- // }
- // else
- // {
- // [self.overlay setBackgroundColor:kNavBarThemeColor];
- // }
- //}
- - (void)followScrollView:(UIView *)scrollableView
- {
- if (self.scrollableView)
- {
- [self.scrollableView removeGestureRecognizer:self.panGesture];
- }
- self.scrollableView = scrollableView;
- self.panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
- [self.panGesture setMaximumNumberOfTouches:1];
- [self.panGesture setDelegate:self];
- [self.scrollableView addGestureRecognizer:self.panGesture];
-
- CGRect frame = self.navigationController.navigationBar.frame;
- frame.origin = CGPointZero;
-
- // self.overlay = [self crateOverlay:frame];
-
- // [self configOverlay];
- }
- - (UIView *)crateOverlay:(CGRect)frame
- {
- return [[UIView alloc] initWithFrame:frame];
- }
- //- (void)configOverlay
- //{
- // [self setBarTintColor];
- //
- //
- // [self.overlay setUserInteractionEnabled:NO];
- // [self.navigationController.navigationBar addSubview:self.overlay];
- // [self.overlay setAlpha:0];
- //}
- - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
- {
- return YES;
- }
- #define kNavgationBarOffset 44
- #define kStatusHeight 20
- - (void)onScrollUp:(CGFloat)delta
- {
- CGRect frame;
- if (self.isCollapsed)
- {
- return;
- }
-
- frame = self.navigationController.navigationBar.frame;
-
- if (frame.origin.y - delta < -kNavgationBarOffset)
- {
- delta = frame.origin.y + kNavgationBarOffset;
- }
-
- frame.origin.y = MAX(-kNavgationBarOffset, frame.origin.y - delta);
- self.navigationController.navigationBar.frame = frame;
-
- if (frame.origin.y == -kNavgationBarOffset)
- {
- self.isCollapsed = YES;
- self.isExpanded = NO;
- }
-
- [self updateSizingWithDelta:delta];
-
- if ([self.scrollableView isKindOfClass:[UIScrollView class]])
- {
- [(UIScrollView *)self.scrollableView setContentOffset:CGPointMake(((UIScrollView*)self.scrollableView).contentOffset.x, ((UIScrollView*)self.scrollableView).contentOffset.y - delta)];
- }
-
- [self hideTabbar];
-
- [self layoutOnScrollUp];
- }
- - (void)showTabbar
- {
- if (_handleTabbar && self.tabBarController.tabBar.translucent)
- {
-
- if (self.tabBarController.tabBar.hidden)
- {
- self.tabBarController.tabBar.hidden = NO;
- }
- }
- }
- - (void)onScrollDown:(CGFloat)delta
- {
- CGRect frame;
- if (self.isExpanded)
- {
- return;
- }
-
- frame = self.navigationController.navigationBar.frame;
-
- if (frame.origin.y - delta > kStatusHeight)
- {
- delta = frame.origin.y - kStatusHeight;
- }
- frame.origin.y = MIN(kStatusHeight, frame.origin.y - delta);
- self.navigationController.navigationBar.frame = frame;
-
- if (frame.origin.y == kStatusHeight)
- {
- self.isExpanded = YES;
- self.isCollapsed = NO;
- }
-
- [self updateSizingWithDelta:delta];
-
- [self showTabbar];
-
- [self layoutOnScrollDown];
- }
- - (void)layoutOnScrollUp
- {
-
- }
- - (void)layoutOnScrollDown
- {
-
- }
- - (void)hideTabbar
- {
- if (_handleTabbar && self.tabBarController.tabBar.translucent)
- {
- if (!self.tabBarController.tabBar.hidden)
- {
- self.tabBarController.tabBar.hidden = YES;
- }
- }
- }
- #define kScrollHorOffset 20
- - (void)handlePan:(UIPanGestureRecognizer *)gesture
- {
- switch (gesture.state)
- {
- case UIGestureRecognizerStateChanged:
- {
- CGPoint translation = [gesture translationInView:[self.scrollableView superview]];
- float delta = self.lastContentOffset.y - translation.y;
- self.lastContentOffset = translation;
-
- if (delta == 0)
- {
- return;
- }
-
- if (delta > 0)
- {
- [self onScrollUp:delta];
- }
- else
- {
- [self onScrollDown:delta];
- }
- }
-
- break;
- case UIGestureRecognizerStateEnded:
- {
- self.lastContentOffset = CGPointZero;
- [self checkForPartialScroll];
- }
- break;
- default:
- break;
- }
-
- }
- - (void)checkForPartialScrollEnd
- {
-
- }
- - (void)checkForPartialScroll
- {
- CGFloat pos = self.navigationController.navigationBar.frame.origin.y;
- __block CGFloat delta = 0;
- // Get back down
- if (pos >= 0)
- {
- [UIView animateWithDuration:0.2 animations:^{
- CGRect frame;
- frame = self.navigationController.navigationBar.frame;
- delta = frame.origin.y - kStatusHeight;
- frame.origin.y = MIN(kStatusHeight, frame.origin.y - delta);
- self.navigationController.navigationBar.frame = frame;
-
- self.isExpanded = YES;
- self.isCollapsed = NO;
- } completion:^(BOOL finished) {
- [self updateSizingWithDelta:delta];
- [self checkForPartialScrollEnd];
- }];
- }
- else
- {
- // And back up
- [UIView animateWithDuration:0.2 animations:^{
- CGRect frame;
- frame = self.navigationController.navigationBar.frame;
- CGFloat delta = frame.origin.y + kNavgationBarOffset;
- frame.origin.y = MAX(-kNavgationBarOffset, frame.origin.y - delta);
- self.navigationController.navigationBar.frame = frame;
-
- self.isExpanded = NO;
- self.isCollapsed = YES;
- } completion:^(BOOL finished) {
- [self updateSizingWithDelta:delta];
- [self checkForPartialScrollEnd];
- }];
- }
- }
- - (void)backToOrignal
- {
- // 如果navigationbar 或 tab隐藏时进行push,会有界面乱的情况
- [self onScrollDown:-kNavgationBarOffset];
- self.lastContentOffset = CGPointZero;
- [self checkForPartialScroll];
-
- }
- - (void)updateSizingWithDelta:(CGFloat)delta
- {
- DebugLog(@"delta = %f", delta);
-
- CGRect frame = self.navigationController.navigationBar.frame;
-
- float alpha = (frame.origin.y + kNavgationBarOffset) / frame.size.height;
- // [self.overlay setAlpha:1 - alpha];
- self.navigationController.navigationBar.tintColor = [self.navigationController.navigationBar.tintColor colorWithAlphaComponent:alpha];
-
-
- UIView *scrollSuperView = self.scrollableView.superview;
- frame = scrollSuperView.frame;
- frame.origin.y = self.navigationController.navigationBar.frame.origin.y + self.navigationController.navigationBar.frame.size.height;
- frame.size.height = frame.size.height + delta;
- scrollSuperView.frame = frame;
-
- frame = self.scrollableView.layer.frame;
- frame.size.height += delta;
- self.scrollableView.layer.frame = frame;
- self.scrollableView.frame = frame;
-
-
- // DebugLog(@"=====>>>>>>>>>>on scroll");
- }
- @end
- #endif
|