iOS 手势滑动返回功能

释放双眼,带上耳机,听听看~!

方法一

BaseViewController 是要添手势的控制器
BaseViewController.m

 

#import "BaseViewController.h"

#import "POPAnimation.h"

@interface
BaseViewController ()<UINavigationControllerDelegate>

{

    UIPercentDrivenInteractiveTransition *_interactiveTransition;

}

@end

@implementation BaseViewController

- (void)viewDidLoad {

    [superviewDidLoad];

    self.view.backgroundColor = [UIColorgrayColor];

   
//该控制器的view添加拖动手势

    self.navigationController.delegate
= self;
// 设置navigationController的代理为self,并实现其代理方法

    self.view.userInteractionEnabled =
YES;

    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizeralloc]
initWithTarget:selfaction:@selector(backHandle:)];

    [self.view
addGestureRecognizer:panGesture];

}

- (void)backHandle:(UIPanGestureRecognizer *)recognizer

{

    [selfcustomControllerPopHandle:recognizer];

}


- (void)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 = [[UIPercentDrivenInteractiveTransitionalloc]
init];

        [self.navigationControllerpopViewControllerAnimated:YES];

    }

    elseif(recognizer.state ==
UIGestureRecognizerStateChanged)

    {

        [_interactiveTransitionupdateInteractiveTransition:process]; //
更新手势完成度

    }

    elseif(recognizer.state ==
UIGestureRecognizerStateEnded ||recognizer.state ==
UIGestureRecognizerStateCancelled)

    {

       
// 手势结束时,若进度大于0.5就完成pop动画,否则取消

        if(process >
0.5)

        {

            [_interactiveTransitionfinishInteractiveTransition];

        }

        else

        {

            [_interactiveTransitioncancelInteractiveTransition];

        }

        _interactiveTransition =
nil;

    }

}

// 代理方法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;

}

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/

@end

POPAnimation.m

#import "POPAnimation.h"


@interface POPAnimation ()

@end

@implementation POPAnimation

// 实现两个协议的方法


// 返回动画执行的时间

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext

{

    return 0.25;

}
//

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext

{

    __blockUIViewController *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移动到屏幕最右侧

    [UIViewanimateWithDuration:duration animations:^{

        fromVC.view.transform =
CGAffineTransformMakeTranslation([UIScreenmainScreen].bounds.size.width,
0);

    } completion:^(BOOL finished) {

       
// 当动画执行完时,这个方法必须要调用,否则系统会认为你的其余操作都在动画执行过程中

        [transitionContext completeTransition:![transitionContext
transitionWasCancelled]];

    }];

}

@end

 

 

 

方法二(简洁)

SecViewController.m

#import "SecViewController.h"

@interface
SecViewController ()<UIGestureRecognizerDelegate>

@end

@implementation SecViewController

- (void)viewDidLoad {

    [superviewDidLoad];

    self.view.backgroundColor = [UIColorredColor];

    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;

}

// 什么时候调用,每次触发手势之前都会询问下代理方法,是否触发

// 作用:拦截手势触发

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer

{

   
// 当当前控制器是根控制器时,不可以侧滑返回,所以不能使其触发手势

    if(self.navigationController.childViewControllers.count
== 1)

    {

        return NO;

    }

    return
YES;

}

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/

@end
这样就ok啦

 

人已赞赏
iOS文章

iOS 填写完身份证后 自动算出 出生日期

2020-4-30 23:01:53

iOS文章

iOS 常写在pch中的设备区分

2020-5-1 0:17:39

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索