iOS7以后,导航控制器,自带了从屏幕左边缘右滑返回的手势功能。但是,如果自定义了导航栏返回按钮,这项功能就失效了,需要自行实现。又如果需要修改手势触发范围,还是需要自行实现。广泛应用的一种实现方案是,采用私有变量和Api,完成手势交互和返回功能,自定义手势触发条件和额外功能。另 ...
iOS7以后,导航控制器,自带了从屏幕左边缘右滑返回的手势功能。
但是,如果自定义了导航栏返回按钮,这项功能就失效了,需要自行实现。又如果需要修改手势触发范围,还是需要自行实现。
广泛应用的一种实现方案是,采用私有变量和Api,完成手势交互和返回功能,自定义手势触发条件和额外功能。
另一种实现方案是,采用UINavigationController的代理方法实现交互和动画:
- (nullable id <UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController
interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>) animationController NS_AVAILABLE_IOS(7_0);
- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC NS_AVAILABLE_IOS(7_0);
前者,特点是便捷,但是只能使用系统定义的交互和动画;后者,特点是高度自定义,但是需要额外实现交互协议和动画协议。
采用私有变量和Api,实现右滑返回手势功能。
先看最核心的逻辑:
- (void)base_pushViewController:(UIViewController *)viewController animated:(BOOL)animated{ if (![self.interactivePopGestureRecognizer.view.gestureRecognizers containsObject:self.base_panGestureRecognizer]) { [self.interactivePopGestureRecognizer.view addGestureRecognizer:self.base_panGestureRecognizer]; //使用KVC获取私有变量和Api,实现系统原生的pop手势效果 NSArray *internalTargets = [self.interactivePopGestureRecognizer valueForKey:@"targets"]; id internalTarget = [internalTargets.firstObject valueForKey:@"target"]; SEL internalAction = NSSelectorFromString(@"handleNavigationTransition:"); self.base_panGestureRecognizer.delegate = [self base_panGestureRecognizerDelegateObject]; [self.base_panGestureRecognizer addTarget:internalTarget action:internalAction]; self.interactivePopGestureRecognizer.enabled = NO; } [self base_setupViewControllerBasedNavigationBarAppearanceIfNeeded:viewController]; if (![self.viewControllers containsObject:viewController]) { [self base_pushViewController:viewController animated:animated]; }}
原标题:App开发流程之右滑返回手势功能
关键词:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。