UITapGestureRecognizer

UITapGestureRecognizer

UITapGestureRecognizer

IOS的手势非常多, 但是特别容易其他视图起冲突的手势,要数UITapGestureRecognizer

于是有了gestureRecognizerShouldBegin:这个方法判断是否应该执行触摸事件

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
BOOL flag = [super gestureRecognizerShouldBegin:gestureRecognizer];
if (flag) {
NSLog(@"gestureRecognizerShouldBegin:");
}
return flag;
}

但是当有多个触摸事件时,我们可能就无所适从

别担心, 当父视图和子视图同时拥有触摸事件时,

如果点击到子视图, 那么不会执行父视图的方法

2. 手势不能共用,手势的监听方法也不能共用

错误的做法:

//为下定日期和交货日期设置手势
UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openSystemDatePickerView:)];
[_DateTF addGestureRecognizer:tap2];
[_PayDateTF addGestureRecognizer:tap2];

这里有两个控件设置了同一个手势, 但是一个手势只能被一个控件拥有,

像上面这种做法, 只有_PayDateTF得到手势, _DateTF没有手势

正确的做法应该是:

正确的做法: 
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openSystemDatePickerView:)];
[_DateTF addGestureRecognizer:tap1]; //为下定日期和交货日期设置手势
UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openSystemDatePickerView:)];
[_PayDateTF addGestureRecognizer:tap2];
04-14 16:48