美文网首页swift
侧滑返回与scrollView冲突

侧滑返回与scrollView冲突

作者: Moker_C | 来源:发表于2017-12-01 13:25 被阅读12次

    建一个scrollView的分类(category),重写代理方法让手势能够传递下去

    在.m文件中

    #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

    //此方法返回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 =0.13*SCREEN_WIDTH;

    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];

    //这是允许每张图片都可实现滑动返回

    int temp1 = location.x;

    int temp2 =SCREEN_WIDTH;

    NSInteger XX = temp1 % temp2;

    if (point.x >0 && XX < location_X) {

    return YES;

    }

    //下面的是只允许在第一张时滑动返回生效

    //            if (point.x > 0 && location.x < location_X && self.contentOffset.x <= 0) {

    //                return YES;

    //            }

    }

    }

    return NO;

    }

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

    if ([self panBack:gestureRecognizer]) {

    return NO;

    }

    return YES;

    }

    相关文章

      网友评论

        本文标题:侧滑返回与scrollView冲突

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