xml地图|网站地图|网站标签 [设为首页] [加入收藏]

iOS使其支持侧滑返回

来源:http://www.ccidsi.com 作者:最新解决方案 人气:132 发布时间:2020-03-21
摘要:iOS7从今未来系统提供了侧滑手势(interactivePopGestureRecognizer),即从荧屏右侧边缘滑起会pop回导航调整器栈的上个viewController。 不过只要您自定义了回到按键,系统自带的侧滑重返作用

iOS7从今未来系统提供了侧滑手势(interactivePopGestureRecognizer),即从荧屏右侧边缘滑起会pop回导航调整器栈的上个viewController。不过只要您自定义了回到按键,系统自带的侧滑重返作用会失灵。那会儿内需加多底下的代码解决:

self.navigationController.interactivePopGestureRecognizer.delegate = self;

缺点:

  • 非得从显示屏边缘左边滑起才会接触;
  • 纵然自定义导航调整器恐怕自定义重临按键,就能够失效。

那是苹果官方在WWDC上提倡的情势,灵活性高。能够中度自定义push和pop转场动漫。这种措施须要大家深透完成侧滑重临,那大家的思绪正是:

  • 先给view增加三个UIPanGestureRecognizer手势;
  • 再自定义该手势的触及方法,该方法里完结了侧滑。
1. 先创建四个BaseViewController,给该调节器的view增加拖动手势;
- viewDidLoad{ [super viewDidLoad]; self.navigationController.delegate = self; // 设置navigationController的代理为self,并实现其代理方法 self.view.userInteractionEnabled = YES; UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(backHandle:)]; [self.view addGestureRecognizer:panGesture];}- backHandle:(UIPanGestureRecognizer *)recognizer{ [self customControllerPopHandle:recognizer];}
2.侧滑手势会接触那几个回调方法;
- customControllerPopHandle:(UIPanGestureRecognizer *)recognizer{ if(self.navigationController.childViewControllers.count == 1) { return; } // _interactiveTransition就是代理方法2返回的交互对象,我们需要更新它的进度来控制POP动画的流程。(以手指在视图中的位置与屏幕宽度的比例作为进度) CGFloat process = [recognizer translationInView:self.view].x/self.view.bounds.size.width; process = MIN(1.0, MAX(0.0, process)); if(recognizer.state == UIGestureRecognizerStateBegan) { // 此时,创建一个UIPercentDrivenInteractiveTransition交互对象,来控制整个过程中动画的状态 _interactiveTransition = [[UIPercentDrivenInteractiveTransition alloc] init]; [self.navigationController popViewControllerAnimated:YES]; } else if(recognizer.state == UIGestureRecognizerStateChanged) { [_interactiveTransition updateInteractiveTransition:process]; // 更新手势完成度 } else if(recognizer.state == UIGestureRecognizerStateEnded ||recognizer.state == UIGestureRecognizerStateCancelled) { // 手势结束时,若进度大于0.5就完成pop动画,否则取消 if(process > 0.5) { [_interactiveTransition finishInteractiveTransition]; } else { [_interactiveTransition cancelInteractiveTransition]; } _interactiveTransition = nil; }}
3.兑现UINavigationControllerDelegate的三个切磋格局,分别重返自定义动漫必要的多个首要目的;
// 代理方法1:// 返回一个实现了UIViewControllerAnimatedTransitioning协议的对象 ,即完成转场动画的对象- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{ if(operation == UINavigationControllerOperationPop) // 若operation是pop,就返回我们自定义的转场动画对象 { return [[POPAnimation alloc] init]; } return nil;}// 代理方法2// 返回一个实现了UIViewControllerInteractiveTransitioning协议的对象,即完成动画交互的对象- (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController{ if([animationController isKindOfClass:[POPAnimation class]]) { return _interactiveTransition; } return nil;}
4.创建一个自定义动画类:POPAnimation。那是自定义动漫的为主
  • 自定义动漫类,即五个得以完成了UIViewControllerAnimatedTransitioning合同的类。
  • 兑现该公约的三个措施,四个回去动漫试行时间,三个自定义动画。
#import "POPAnimation.h"@interface POPAnimation ()@end@implementation POPAnimation// 实现两个协议的方法// 返回动画执行的时间- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{ return 0.25;}//- animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{ __block UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; // 动画来自哪个vc UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; // 转场到哪个vc // 转场动画是两个控制器视图的动画,需要一个containerView作为“舞台” UIView *containerView = [transitionContext containerView]; [containerView insertSubview:toVC.view belowSubview:fromVC.view]; NSTimeInterval duration = [self transitionDuration:transitionContext]; // 获取动画执行时间 // 执行动画,让fromVC的view移动到屏幕最右侧 [UIView animateWithDuration:duration animations:^{ fromVC.view.transform = CGAffineTransformMakeTranslation([UIScreen mainScreen].bounds.size.width, 0); } completion:^(BOOL finished) { // 当动画执行完时,这个方法必须要调用,否则系统会认为你的其余操作都在动画执行过程中 [transitionContext completeTransition:![transitionContext transitionWasCancelled]]; }]; }@end

图片 1pop.gif

iOS7未来是有侧滑重回击势成效的。注意,也便是说系统已经定义了一种手势,而且给这几个手势已经加多了八个接触方法。不过,系统的这么些手势的触发条件是必得从显示屏左侧缘开头滑动。大家取巧的格局是团结写叁个支撑全屏滑动的手势,而其触发方法种类已经有,没须要本身达成pop的卡通,所以直接就把系统的触及管理格局作为大家友好定义的手势的拍卖方法。

#import "ViewController.h"@interface ViewController ()<UIGestureRecognizerDelegate>@end@implementation ViewController- viewDidLoad{ [super viewDidLoad]; id target = self.navigationController.interactivePopGestureRecognizer.delegate; // handleNavigationTransition:为系统私有API,即系统自带侧滑手势的回调方法,我们在自己的手势上直接用它的回调方法 UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)]; panGesture.delegate = self; // 设置手势代理,拦截手势触发 [self.view addGestureRecognizer:panGesture]; // 一定要禁止系统自带的滑动手势 self.navigationController.interactivePopGestureRecognizer.enabled = NO;}// 什么时候调用,每次触发手势之前都会询问下代理方法,是否触发// 作用:拦截手势触发- gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{ // 当当前控制器是根控制器时,不可以侧滑返回,所以不能使其触发手势 if(self.navigationController.childViewControllers.count == 1) { return NO; } return YES;}@end

方案二涉及到了自定义转场动漫的东西。关于自定义转场动漫有多个大旨的商量:

  • UIViewControllerAnimatedTransitioning:信守该左券的指标,便是大家自定义的卡通片;
  • UIViewControllerInteractiveTransitioning:遵循该公约贯彻动漫可交互作用性。可是貌似大家一贯运用系统UIPercentDrivenInteractiveTransition类,不需自定义。
  • UIViewControllerContextTransitioning:服从该合同,定义了转场时索要的元数据。常常不需和睦定义。

本文由68399皇家赌场发布于最新解决方案,转载请注明出处:iOS使其支持侧滑返回

关键词: 68399皇家赌场 iOS 使其

最火资讯