方法一
#import "BaseViewController.h"
#import "POPAnimation.h"
@interface BaseViewController ()<UINavigationControllerDelegate>
{
UIPercentDrivenInteractiveTransition *_interactiveTransition;
}
@end
@implementation BaseViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
//该控制器的view添加拖动手势
self.navigationController.delegate = self; // 设置navigationController的代理为self,并实现其代理方法
self.view.userInteractionEnabled = YES;
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(backHandle:)];
[self.view addGestureRecognizer:panGesture];
}
- (void)backHandle:(UIPanGestureRecognizer *)recognizer
{
[self customControllerPopHandle: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 = [[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;
}
}
// 代理方法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 {
[super didReceiveMemoryWarning];
// 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
{
__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
方法二(简洁)
SecViewController.m
#import "SecViewController.h"
@interface SecViewController ()<UIGestureRecognizerDelegate>
@end
@implementation SecViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
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 {
[super didReceiveMemoryWarning];
// 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