UIScrollView的左右滑动和侧滑手势冲突的解决办法

转载自:https://blog.csdn.net/kst_123/article/details/77762811

当ViewController中添加了一个全屏的UIScrollView的时候,UIScrollView的左滑手势会和系统的左滑返回冲突,系统的左滑返回将会失效。解决办法如下:

自定义一个CustomScrollView继承于UIScrollView,然后重写一下gestureRecognizerShouldBegin方法。

左边侧滑:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    CGPoint velocity = [(UIPanGestureRecognizer *)gestureRecognizer velocityInView:self];
    CGPoint location = [gestureRecognizer locationInView:self];
    
    if (velocity.x > 0.0f&&(int)location.x%(int)[UIScreen mainScreen].bounds.size.width<60) {
        return NO;
    }
    return YES;
}

右边侧滑:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    CGPoint velocity = [(UIPanGestureRecognizer *)gestureRecognizer velocityInView:self];
    CGPoint location = [gestureRecognizer locationInView:self];
    
    if (velocity.x > 0.0f&&(int)location.x%(int)[UIScreen mainScreen].bounds.size.width>[UIScreen mainScreen].bounds.size.width-60) {
        return NO;
    }
    return YES;
}

奉上一个自定义的UIScrollView的代码:

DXCustomScrollView.h

#import <UIKit/UIKit.h>

@interface DXCustomScrollView : UIScrollView

@end

DXCustomScrollView.m

#import "DXCustomScrollView.h"

@interface DXCustomScrollView()

@end

@implementation DXCustomScrollView

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    CGPoint velocity = [(UIPanGestureRecognizer *)gestureRecognizer velocityInView:self];
    CGPoint location = [gestureRecognizer locationInView:self];
    
    if (velocity.x > 0.0f&&(int)location.x%(int)[UIScreen mainScreen].bounds.size.width<60) {
        return NO;
    }
    return YES;
}

@end
原文地址:https://www.cnblogs.com/wobuyayi/p/9502714.html