博客> 关于侧滑popToViewController的问题与处理
关于侧滑popToViewController的问题与处理
2018-09-22 13:10 评论:0 阅读:886 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; }

//locationX可自己定义,其代表的是滑动返回距左边的手势响应范围 - (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>

@property (nonatomic, strong) UIPanGestureRecognizer * panGesture; @end

@implementation FHNavigationController

_- (void)viewDidLoad {

[super viewDidLoad];
self.panGesture.delegate = self;
[self.view addGestureRecognizer:self.panGesture];
self.interactivePopGestureRecognizer.enabled = NO;

} _-(UIPanGestureRecognizer *)panGesture{

_ if (!_panGesture) {

    id target = self.interactivePopGestureRecognizer.delegate;
    _panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
    _panGesture.delegate = self;

}
return _panGesture;

}

// 控制 滑动范围 _-(BOOL)gestureRecognizer:(UIGestureRecognizer )gestureRecognizer shouldReceiveTouch:(UITouch )touch{

//返回触摸点在视图中的当前在屏幕中的坐标 CGPoint point = [touch locationInView:[UIApplication sharedApplication].delegate.window]; CGFloat x = point.x; CGFloat y = point.y; if (point.x > PopGesturWidh) { // 滑动范围控制 return NO; } return YES; } // 侧滑 最后一个 控制器的处理 - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {

if (self.childViewControllers.count == 1) {
    return NO;
}
return YES;

}

//关闭 侧滑 手势 - (void)hide { panGesture.enabled = NO; } //打开侧滑 手势 - (void)show_ { _panGesture.enabled = YES; }

收藏
1
sina weixin mail 回到顶部