美文网首页
自定义全屏返回手势及解决scrollview冲突

自定义全屏返回手势及解决scrollview冲突

作者: nenhall | 来源:发表于2017-07-31 17:08 被阅读148次
@interface BKNavigationController ()<UIGestureRecognizerDelegate,UINavigationControllerDelegate>
@property(nonatomic, strong) NSMutableArray *interactivePopGestureBlackList;
@end

@implementation BKNavigationController
- (void)viewDidLoad {
    [self addPopGestureRecognizer];
}

- (void)addPopGestureRecognizer {
    id target = self.interactivePopGestureRecognizer.delegate;
    SEL handler = NSSelectorFromString(@"handleNavigationTransition:");
    //  获取添加系统边缘触发手势的View
    UIView *targetView = self.interactivePopGestureRecognizer.view;
    
    //  创建pan手势 作用范围是全屏
    UIPanGestureRecognizer * fullScreenGes = [[UIPanGestureRecognizer alloc] initWithTarget:target action:handler];
    fullScreenGes.delegate = self;
    [targetView addGestureRecognizer:fullScreenGes];
    
    // 关闭边缘触发手势 防止和原有边缘手势冲突
    [self.interactivePopGestureRecognizer setEnabled:NO];
    
}


//拦截自定义手势的触发,并记录触发手势的开始位置
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
    
    // 解决与tableView左滑功能的冲突
    CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view];
    if (translation.x <= 0) {
        return NO;
    }
    
    //导航控制器的跟控制器不需要返回侧滑手势
    if (self.childViewControllers.count == 1) {
        return NO;
    }
    
    // 根据具体控制器对象决定是否开启全屏右滑返回
    for (Class viewController in self.interactivePopGestureBlackList) {
        if ([[self topViewController] isKindOfClass:viewController]) {
            return NO;
        }
    }
  
    return YES;
}

<br />
<br />

解决与scrollview横向滚动的手势冲突,我这时是写了一个UIScrollView的类扩展,统一处理了,你也可以自建一个继承UIScrollView的view,然后在自建的类中实现以下两方法

#import "UIScrollView+Extension.h"

@implementation UIScrollView (Extension)
/** 
下面这两个方法没用,这里只是记录下
////返回yes-前面失效后面生效    返回no-前面生效后面失效
//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
//    return ![gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class];
//}

////返回yes-前面生效后面失效    返回no-前面失效后面生效
//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
//    return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class];
//}
*/

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    //如果单单在这里处理的话,只能解决scrollview横向滚动,但无法侧滑返回上一页,所以需要重写`- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer`
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIScreenEdgePanGestureRecognizer class]]){
        return YES;
    } else {
        return  NO;
    }
}


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

- (BOOL)panBack:(UIGestureRecognizer *)gestureRecognizer {
    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];
            NSLog(@"%@",NSStringFromCGPoint(location));
// kScreenWidth这个值你可以自自己定义,即在多少的位置的时候触发
            if (point.x > 0 && location.x < kScreenWidth && self.contentOffset.x <= 0) {
                return YES;
            }
        }
    }
    return NO;
}

@end

相关文章

网友评论

      本文标题:自定义全屏返回手势及解决scrollview冲突

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