手势过滤和手势屏蔽

如何屏蔽指定区域利用 tap 手势, 利用 tap 手势, 在其代理方法中进行区域限制, 然后根据需要返回 YES / NO。其实就是获取在屏幕上的点击位置, 对位置进行局域判断, 过滤掉一些你不想要的手势。

核心的代理方法是这个: 

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
       CGPoint position = [touch locationInView:_bottomView];
        //屏蔽手势
        if (判断区域是否符合) {
            return NO;
        }
    }
    return YES;
}

 

例子

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *bottomView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapEvent)];
    tap.delegate = self;
    [_bottomView addGestureRecognizer:tap];

}

- (void)tapEvent
{
    NSLog(@"点击了我");
}
//屏蔽某些位置点击是否可用
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
       CGPoint position = [touch locationInView:_bottomView];
        //屏蔽手势
        if (position.x < 150) {
            return NO;
        }
    }
    return YES;
}

另外在公开 API 中 , GestureRecognizer总共6个代理方法, 如下:

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return YES;
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return YES;
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceivePress:(UIPress *)press
{
    return YES;
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return NO;
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

最后附上私有 API 中的两个代理方法, 有兴趣的可以看一看, 审核是否会被拒, 这个本人没有试验过, 不可得知:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer canBePreventedByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer  { 
BOOL result = YES;
// something you do 
return result; 
} 

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer canPreventGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer  { 
BOOL result = YES;
// something you do 
return result; 
} 

你可能感兴趣的:(Objective-C)