美文网首页
一键解决自定义返回按钮侧滑pop手势失效及VC子视图中包含UIS

一键解决自定义返回按钮侧滑pop手势失效及VC子视图中包含UIS

作者: OCeasyLife | 来源:发表于2017-12-15 00:31 被阅读0次

    新增:

    解决

    1.解决其他的对viewController中手势通过实现手势代理方法处理,造成手势方法不识别,而出现的崩溃。----2018.09.01

    2.WKWebView.allowsBackForwardNavigationGestures = YES 时,WKWebView返回手势和侧滑手势的冲突----2018.04.18

    解决思路

    >使用自定义手势替换系统的侧滑手势

    >target 及 action 不变

    >将自定义手势添加到UIVIewController的view上,实现手势在不同vc中的不同响应

    解决过程

    1.使用自定义UIPanGestureRecognizer替换系统的手势

    //设置系统手势不可用

    self.navigationController.interactivePopGestureRecognizer.enabled = NO;

    //获取手势的代理对象

    id target = self.navigationController.interactivePopGestureRecognizer.delegate;

    //获取手势响应的方法

    SEL backGestureSelector = NSSelectorFromString(@"handleNavigationTransition:");

    panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:target action:backGestureSelector];

    2.添加并设置手势的代理为当前VC

    if ([self.navigationController.  respondsToSelector:@selector(interactivePopGestureRecognizer)]) {

    //使用自定义的手势替换系统的侧边触发手势

    //设置手势的代理

    [self syPanGesture].delegate = (self.sy_interactivePopDisabled == YES) ? nil : self;

    //将自定义手势添加到vc的view上

    [self.view addGestureRecognizer:[self syPanGesture]];

      }

    3.UIScrollView 手势的响应

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

    //当panGesture相应失败时,才响应scrollView的拖动手势

    [otherGestureRecognizer requireGestureRecognizerToFail:gestureRecognizer];

    return YES;

    }

    4.UISlider的响应

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{

    //当拖动的是slider时,该事件不让panGesture手势响应

    if ([touch.view isKindOfClass:[UISlider class]]) {

    return NO;

    }

    return YES;

    }

    5.拖动中其他状态的处理

    - (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {

    if ([[self.navigationController valueForKey:@"_isTransitioning"] boolValue]) {

    return NO;

    }

    if ([self.navigationController.transitionCoordinator isAnimated]) {

    return NO;

    }

    if (self.navigationController.viewControllers.count < 2) {

    return NO;

    }

    return YES;

    }

    6.实现全屏和侧边响应

    - (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {

    if ([[self.navigationController valueForKey:@"_isTransitioning"] boolValue]) {

    return NO;

    }

    if ([self.navigationController.transitionCoordinator isAnimated]) {

    return NO;

    }

    if (self.navigationController.viewControllers.count < 2) {

    return NO;

    }

    // 侧滑手势触发位置

    CGPoint location = [gestureRecognizer locationInView:self.view];

    CGPoint offSet = [gestureRecognizer translationInView:gestureRecognizer.view];

    //触发宽度,

    CGFloat maxLocationX = self.sy_isFullPopGesture == YES ? CGRectGetWidth(self.view.bounds) : 40.f;

    //当是全屏返回手势时,使用整个宽度

    BOOL ret = (0 < offSet.x && location.x <= maxLocationX);

    return ret;

    }

    代码Demo

    GitHub :SYPopGesture

    参考资料

    1.全屏返回手势 FDFullscreenPopGesture

    2.一行代码,让你的应用中UIScrollView的滑动与侧滑返回并存 TZPopGesture

    相关文章

      网友评论

          本文标题:一键解决自定义返回按钮侧滑pop手势失效及VC子视图中包含UIS

          本文链接:https://www.haomeiwen.com/subject/pdmqottx.html