iOS处理单双击手势冲突问题

下面是.m文件里面的内容:(主要是requireGestureRecognizerToFail:)

- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 200, 110, 200)];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.userInteractionEnabled = YES;
[self.view addSubview:imageView];
UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];

[singleTapGestureRecognizer setNumberOfTapsRequired:1];
[imageView addGestureRecognizer:singleTapGestureRecognizer];

UITapGestureRecognizer *doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTap:)];
[doubleTapGestureRecognizer setNumberOfTapsRequired:2];
[imageView addGestureRecognizer:doubleTapGestureRecognizer];

//这行很关键,意思是只有当没有检测到doubleTapGestureRecognizer或者检测doubleTapGestureRecognizer失败,singleTapGestureRecognizer才有效
[singleTapGestureRecognizer requireGestureRecognizerToFail:doubleTapGestureRecognizer];
}

- (void)singleTap:(UIGestureRecognizer*)gestureRecognizer
{
NSLog(@"-----singleTap-----");
[self.view setBackgroundColor:[UIColor redColor]];
}

- (void)doubleTap:(UIGestureRecognizer*)gestureRecognizer
{
NSLog(@"-----doubleTap-----");
[self.view setBackgroundColor:[UIColor blueColor]];
}

你可能感兴趣的:(iOS处理单双击手势冲突问题)