电竞比分网-中国电竞赛事及体育赛事平台

分享

再談iOS 7的手勢(shì)滑動(dòng)返回功能

 quasiceo 2015-04-24
分類: iOS 2014-06-03 20:00 12793人閱讀 評(píng)論(2) 收藏 舉報(bào)

之前隨手寫過一篇《使用UIScreenEdgePanGestureRecognizer實(shí)現(xiàn)swipe to pop效果》,挺粗糙的。

現(xiàn)在使用默認(rèn)模板創(chuàng)建的iOS App都支持手勢(shì)返回功能,如果導(dǎo)航欄的返回按鈕是自定義的那么則會(huì)失效,也可以參考這里手動(dòng)設(shè)置無效。

  1. if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {  
  2.     self.navigationController.interactivePopGestureRecognizer.enabled = NO;  
  3. }  

如果是因?yàn)樽远x導(dǎo)航按鈕而導(dǎo)致手勢(shì)返回失效,那么可以在NavigationController的viewDidLoad函數(shù)中添加如下代碼:
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view.  
  5.       
  6.     __weak typeof (self) weakSelf = self;  
  7.     if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {  
  8.         self.interactivePopGestureRecognizer.delegate = weakSelf;  
  9.     }  
  10. }  

這樣寫了以后就可以通過手勢(shì)滑動(dòng)返回上一層了,但是如果在push過程中觸發(fā)手勢(shì)滑動(dòng)返回,會(huì)導(dǎo)致導(dǎo)航欄崩潰(從日志中可以看出)。針對(duì)這個(gè)問題,我們需要在pop過程禁用手勢(shì)滑動(dòng)返回功能:
  1. - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated  
  2. {    
  3.     // fix 'nested pop animation can result in corrupted navigation bar'  
  4.     if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {  
  5.         self.interactivePopGestureRecognizer.enabled = NO;  
  6.     }  
  7.       
  8.     [super pushViewController:viewController animated:animated];  
  9. }  

  1. - (void)navigationController:(UINavigationController *)navigationController  
  2.        didShowViewController:(UIViewController *)viewController  
  3.                     animated:(BOOL)animated  
  4. {  
  5.     if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {  
  6.         navigationController.interactivePopGestureRecognizer.enabled = YES;  
  7.     }  
  8. }  

除了使用系統(tǒng)默認(rèn)的動(dòng)畫,還可以使用自定義過渡動(dòng)畫(豐滿的文檔):
  1. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController  
  2.                                   animationControllerForOperation:(UINavigationControllerOperation)operation  
  3.                                                fromViewController:(UIViewController *)fromVC  
  4.                                                  toViewController:(UIViewController *)toVC  
  5. {  
  6.     if (operation == UINavigationControllerOperationPop) {  
  7.         if (self.popAnimator == nil) {  
  8.             self.popAnimator = [WQPopAnimator new];  
  9.         }  
  10.         return self.popAnimator;  
  11.     }  
  12.       
  13.     return nil;  
  14. }  
  15.   
  16. - (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController  
  17.                          interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController  
  18. {  
  19.     return self.popInteractionController;  
  20. }  
  21.   
  22. #pragma mark -   
  23.   
  24. - (void)enablePanToPopForNavigationController:(UINavigationController *)navigationController  
  25. {  
  26.     UIScreenEdgePanGestureRecognizer *left2rightSwipe = [[UIScreenEdgePanGestureRecognizer alloc]  
  27.                                                          initWithTarget:self  
  28.                                                          action:@selector(didPanToPop:)];  
  29.     //[left2rightSwipe setDelegate:self];  
  30.     [left2rightSwipe setEdges:UIRectEdgeLeft];  
  31.     [navigationController.view addGestureRecognizer:left2rightSwipe];  
  32.       
  33.     self.popAnimator = [WQPopAnimator new];  
  34.     self.supportPan2Pop = YES;  
  35. }  
  36.   
  37. - (void)didPanToPop:(UIPanGestureRecognizer *)panGesture  
  38. {  
  39.     if (!self.supportPan2Pop) return ;  
  40.       
  41.     UIView *view = self.navigationController.view;  
  42.       
  43.     if (panGesture.state == UIGestureRecognizerStateBegan) {  
  44.         self.popInteractionController = [UIPercentDrivenInteractiveTransition new];  
  45.         [self.navigationController popViewControllerAnimated:YES];  
  46.     } else if (panGesture.state == UIGestureRecognizerStateChanged) {  
  47.         CGPoint translation = [panGesture translationInView:view];  
  48.         CGFloat d = fabs(translation.x / CGRectGetWidth(view.bounds));  
  49.         [self.popInteractionController updateInteractiveTransition:d];  
  50.     } else if (panGesture.state == UIGestureRecognizerStateEnded) {  
  51.         if ([panGesture velocityInView:view].x > 0) {  
  52.             [self.popInteractionController finishInteractiveTransition];  
  53.         } else {  
  54.             [self.popInteractionController cancelInteractiveTransition];  
  55.         }  
  56.         self.popInteractionController = nil;  
  57.     }  
  58. }  

如下這個(gè)代理方法是用來提供一個(gè)非交互式的過渡動(dòng)畫的:
  1. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController  
  2.                                   animationControllerForOperation:(UINavigationControllerOperation)operation  
  3.                                                fromViewController:(UIViewController *)fromVC  
  4.                                                  toViewController:(UIViewController *)toVC  
  5. {  
  6.     if (operation == UINavigationControllerOperationPop) {  
  7.         if (self.popAnimator == nil) {  
  8.             self.popAnimator = [WQPopAnimator new];  
  9.         }  
  10.         return self.popAnimator;  
  11.     }  
  12.       
  13.     return nil;  
  14. }  

而下面這個(gè)代理方法則是提供交互式動(dòng)畫:
  1. - (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController  
  2.                          interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController  
  3. {  
  4.     return self.popInteractionController;  
  5. }  

這兩個(gè)組合起來使用。首先,我們需要有個(gè)動(dòng)畫:
  1. @interface WQPopAnimator : NSObject <UIViewControllerAnimatedTransitioning>  
  2.   
  3. @end  

  1. #import "WQPopAnimator.h"  
  2.   
  3. @implementation WQPopAnimator  
  4.   
  5. - (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext  
  6. {  
  7.     return 0.4;  
  8. }  
  9.   
  10. - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext  
  11. {  
  12.     UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];  
  13.     UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];  
  14.     [[transitionContext containerView] insertSubview:toViewController.view belowSubview:fromViewController.view];  
  15.       
  16.     __block CGRect toRect = toViewController.view.frame;  
  17.     CGFloat originX = toRect.origin.x;  
  18.     toRect.origin.x -= toRect.size.width / 3;  
  19.     toViewController.view.frame = toRect;  
  20.       
  21.     [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{  
  22.         CGRect fromRect = fromViewController.view.frame;  
  23.         fromRect.origin.x += fromRect.size.width;  
  24.         fromViewController.view.frame = fromRect;  
  25.           
  26.         toRect.origin.x = originX;  
  27.         toViewController.view.frame = toRect;  
  28.     } completion:^(BOOL finished) {  
  29.         [transitionContext completeTransition:![transitionContext transitionWasCancelled]];  
  30.     }];  
  31. }  
  32.   
  33. @end  

其次,交互式動(dòng)畫是通過
  1. UIPercentDrivenInteractiveTransition  
來維護(hù)的,在滑動(dòng)過程中根據(jù)滑動(dòng)距離來進(jìn)行更新:
  1. else if (panGesture.state == UIGestureRecognizerStateChanged) {  
  2.         CGPoint translation = [panGesture translationInView:view];  
  3.         CGFloat d = fabs(translation.x / CGRectGetWidth(view.bounds));  
  4.         [self.popInteractionController updateInteractiveTransition:d];  
當(dāng)手勢(shì)結(jié)束時(shí)要做出收尾動(dòng)作:
  1. else if (panGesture.state == UIGestureRecognizerStateEnded) {  
  2.         if ([panGesture velocityInView:view].x > 0) {  
  3.             [self.popInteractionController finishInteractiveTransition];  
  4.         } else {  
  5.             [self.popInteractionController cancelInteractiveTransition];  
  6.         }  
  7.         self.popInteractionController = nil;  
  8.     }  

同樣地,自定義的動(dòng)畫也會(huì)有上面提到的導(dǎo)航欄崩潰問題,也可以通過類似的方法來解決:
  1. - (void)navigationController:(UINavigationController *)navigationController  
  2.        didShowViewController:(UIViewController *)viewController  
  3.                     animated:(BOOL)animated  
  4. {  
  5.     if (viewController == self.navigationController.pushingViewController) {  
  6.         self.supportPan2Pop = YES;  
  7.         self.navigationController.pushingViewController = nil;  
  8.     }  

補(bǔ)充:位于當(dāng)前navgationController的第一個(gè)([0])viewController時(shí)需要設(shè)置手勢(shì)代理,不響應(yīng)。

10
0
主題推薦
ios interface animation 導(dǎo)航 uiview

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多