博客> 关于侧滑popToViewController的问题与处理
关于侧滑popToViewController的问题与处理
1小时前 评论:0 阅读:770 501993785
ios interactivePopGestureRecognizer侧滑问题

做了几年的iOS 开发 一直没有写技术博客的习惯,以至于技术语言表达很枯竭~。 不废话了…… 苹果官方在 iOS7.0 给出了 interactivePopGestureRecognizer 属性 , 侧滑返回 出栈 方式; 一、在使用过程中 很容易与UIViewController控制器中的subview子类视图造成手势冲突; 以下介绍两种最为常见的冲突;

1.冲突一:侧滑返回 与 UIScrollView 的冲突

 在使用scrollview做轮播图时;经常遇到UINavigationController中的侧滑返回手势不响应,由于手势响应未能传递到navigation,导致侧滑pop无反应;

这种情况可以在UIScrollview.m 重写手势的代理方法,进行处理。 解决方法如下:

  • (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { if ([self panBack:gestureRecognizer]) { return NO; } return YES; }

//一句话总结就是此方法返回YES时,手势事件会一直往下传递,不论当前层次是否对该事件进行响应。

  • (BOOL)gestureRecognizer:(UIGestureRecognizer )gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer )otherGestureRecognizer { if ([self panBack:gestureRecognizer]) { return YES; } return NO; }

//location_X可自己定义,其代表的是滑动返回距左边的手势响应范围

  • (BOOL)panBack:(UIGestureRecognizer )gestureRecognizer { int location_X = PopGesturWidh; if (gestureRecognizer == self.panGestureRecognizer) { UIPanGestureRecognizer pan = (UIPanGestureRecognizer *)gestureRecognizer; CGPoint point = [pan translationInView:self]; UIGestureRecognizerState state = gestureRecognizer.state; if (UIGestureRecognizerStateBegan == state ||UIGestureRecognizerStatePossible == state) { CGPoint location = [gestureRecognizer locationInView:self]; if (point.x >0 && location.x < location xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed> 200) { // 滑动范围控制 return NO; } return YES; }

//关闭 侧滑 手势

  • (void)hide { _panGesture.enabled = NO; } //打开侧滑 手势
  • (void)show { _panGesture.enabled = YES; }
收藏
1
sina weixin mail 回到顶部