两种情形的解决方案如下:
Scenario1 : UITableView中的cell点击实现
/*1.遵守UIGestureRecognizerDelegate的协议*/
@interface ****ViewController ()<UIGestureRecognizerDelegate>
@end
/*2.Create GR对象,并且当前controller成为代理*/
-(void)viewDidLoad{
UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClicked:)];
tapGr.delegate = self;
[self.view addGestureRecognizer:tapGr];
}
/*3.实现这个代理方法*/
#pragma mark Delegate for the gestureRecognizer
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
return NO;
}
return YES;
}
Scenario 2 : UICollectionView中的cell点击失效
/*1.遵守UIGestureRecognizerDelegate的协议*/
@interface ****ViewController ()<UIGestureRecognizerDelegate>
@end
/*2.Create GR对象,并且当前controller成为代理*/
-(void)viewDidLoad{
UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClicked:)];
tapGr.delegate = self;
[self.view addGestureRecognizer:tapGr];
}
/*3.实现这个代理方法*/
#pragma mark Delegate for the gestureRecognizer
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
//注意传入的参数是当前的CollectionView对象
if ([touch.view isDescendantOfView:self.currentCollectionView]) {
return NO;
}
return YES;
}
两者的区别就是最后手势代理方法的实现。
网友评论