开发语言有面向对象开发,其实产品交互也有面向对象的概念.譬如APP有一个功能模块它能够通过多个步骤产生一个或多个结果,到达结果界面后返回的指令是回到该模块的入口处.这样就不会重复pop,以增强模块的结果输出.这种情况下,返回时我们常调用的方法是
- (nullable NSArray<__kindof UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;
在点击返回按钮的时候调用该方法能让我们回到指定界面,但如果我们打开了侧滑返回手势,就会发现侧滑依然返回到上一个界面.而翻阅相关文档,并没有能快速达到效果的API,所以需要对UINavigationController和UIViewController的部分方法进行重写.
首先,建一个UIViewController的子类
@interface BaseViewController : UIViewController
@property(nonatomic,strong) UIButton *leftButton;
@end
给返回按钮的添加执行方法
[self.leftButton addTarget:self
action:@selector(popOrDismiss:)
forControlEvents:UIControlEventTouchUpInside];
//默认pop或者dismiss到上一个界面
-(void)popOrDismiss:(UIButton*)buuton
{
if (self.navigationController.viewControllers.count == 1)
{
[self dismissViewControllerAnimated:YES completion:nil];
}
else
{
[self.navigationController popViewControllerAnimated:YES];
}
}
再建一个UINavigationController的子类
@interface DDNavigationViewController ()<UIGestureRecognizerDelegate>
@property (nonatomic,assign) BOOL leftPanUse;//是否为滑动状态
@property (strong, nonatomic)UIPanGestureRecognizer *panGestureRecognizer;//滑动手势
@property (strong, nonatomic)UIImageView *backView;//图片
@property (strong, nonatomic)NSMutableArray *backImgs;//缓存图片数组
@property (assign) CGPoint panBeginPoint;//滑动起始位置
@property (assign) CGPoint panEndPoint;//滑动结束为止
@property (nonatomic,assign) NSInteger toIndex;//目标ViewControlelr的index
@end
系统默认的滑动手势由于暴露的比较少,并且为readonly,所以关闭系统滑动然后新建一个滑动手势
//原生方法无效
self.interactivePopGestureRecognizer.enabled = NO;
//设置新的滑动手势
self.panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognizerAction:)];
self.panGestureRecognizer.delegate = self;
[self.view addGestureRecognizer:self.panGestureRecognizer];
每次push的时候需要将上个界面截屏并将图片存入缓存图片数组中,以便于pop的时候找到对应的ViewController的截屏来保证动画的流畅度.
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
//截图
UIGraphicsBeginImageContextWithOptions([UIScreen mainScreen].bounds.size, YES, 1.0);
[[UIApplication sharedApplication].keyWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.backImgs addObject:img];
[super pushViewController:viewController animated:animated];
}
通过手势代理对滑动手势状态进行监控.滑动开始时让最上层的BaseViewController子类执行点击返回的方法,返回方法中执行DDNavigationViewController的pop方法,这样就能在DDNavigationViewController中拿到目标ViewController
-(void)popOrDismiss:(UIButton*)buuton
{
[self.navigationController popToViewController:viewController animated:YES];
}
- (nullable NSArray<__kindof UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (self.leftPanUse == YES) //正在滑动,得到目标ViewController的index,不执行pop操作
{
self.toIndex = [self.viewControllers indexOfObject:viewController];
return nil;
}
else//滑动结束,执行pop操作,并删除缓存图片
{
NSInteger index = [self.viewControllers indexOfObject:viewController];
[_backImgs removeObjectsInRange:NSMakeRange(index, _backImgs.count - index)];
return [super popToViewController:viewController animated:animated];
}
}
- (void)panGestureRecognizerAction:(UIPanGestureRecognizer*)panGestureRecognizer{
if ([self.viewControllers count] == 1) {
return ;
}
if (panGestureRecognizer.state == UIGestureRecognizerStateBegan) {
NSLog(@"滑动开始");
self.leftPanUse = YES;
BaseViewController *base = [self.viewControllers lastObject];
[base popOrDismiss:nil];
//存放滑动开始的位置
self.panBeginPoint = [panGestureRecognizer locationInView:[UIApplication sharedApplication].keyWindow];
//插入图片
[self insertLastViewFromSuperView:self.view.superview index:self.toIndex];
}else if(panGestureRecognizer.state == UIGestureRecognizerStateEnded){
NSLog(@"滑动结束");
//存放数据
self.panEndPoint = [panGestureRecognizer locationInView:[UIApplication sharedApplication].keyWindow];
self.leftPanUse = NO;
if ((_panEndPoint.x - _panBeginPoint.x) > 150) {
[UIView animateWithDuration:0.3 animations:^{
[self moveNavigationViewWithLenght:[UIScreen mainScreen].bounds.size.width];
} completion:^(BOOL finished) {
[self moveNavigationViewWithLenght:0];
[self removeLastViewFromSuperViewWithIndex:self.toIndex];
UIViewController *viewControler = self.viewControllers[self.toIndex];
[self popToViewController:viewControler animated:NO];
}];
}else{
[UIView animateWithDuration:0.3 animations:^{
[self moveNavigationViewWithLenght:0];
} completion:^(BOOL finished) {
[self removeLastViewFromSuperViewWithIndex:self.toIndex];
}];
}
}else{
//添加移动效果
CGFloat panLength = ([panGestureRecognizer locationInView:[UIApplication sharedApplication].keyWindow].x - _panBeginPoint.x);
if (panLength > 0) {
[self moveNavigationViewWithLenght:panLength];
}
}
}
这样,在Viewcontroller中只要重写popOrDismiss(不重写则默认返回上一界面)就能点击返回和滑动返回到同一个界面.
PS:简书用的不熟,没能绘制逻辑图,导致这块讲的有点绕.后面会补上demo