在给UIAlertView添加手势使点击区域外弹框消失的时候,发现了tap的cancelsTouchesInView方法,官方描述是“A Boolean value affecting whether touches are delivered to a view when a gesture is recognized.”也就是说,可以通过设置这个布尔值,来设置手势被识别时触摸事件是否被传送到视图。
当值为YES的时候,系统会识别手势,并取消触摸事件;为NO的时候,手势识别之后,系统将触发触摸事件。
比如在一个button上添加手势,设置属性cancelsTouchesInView。
- (void)viewDidLoad {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
button.backgroundColor = [UIColor colorWithRed:0.1 green:0.5 blue:0.4 alpha:1];
[supView addSubview:button];
[button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
tap.cancelsTouchesInView = NO;
[button addGestureRecognizer:tap];
}
- (void)tapAction:(UITapGestureRecognizer *)sender {
NSLog(@"tap");
}
- (void)btnAction:(UIButton *)btn {
NSLog(@"button");
}
当cancelsTouchesInView为NO的时候,会分别触发“tapAction:”和“btnAction:”方法;而当cancelsTouchesInView为YES的时候,只会触发“tapAction:”方法。
补充一个关于响应者链的传送门。