实际应用场景中有的时候需要触摸父视图事件同时响应子视图事件,当时困扰了我很长时间。
Gesture Recognizers与事件分发路径的关系
Gesture Recognizers可能会延迟将触摸事件发送到hit-test view上,默认情况下,当Gesture Recognizers识别到手势后,会向hit-test view发送cancel消息,来取消之前发给hit-test view的事件。控制这个流程的是UIGestureRecognizer的三个属性
- cancelsTouchesInView (默认为YES)
- delaysTouchesBegan (默认为NO)
- delaysTouchesEnded (默认为YES)
cancelsTouchesInView为YES,表示当Gesture Recognizers识别到手势后,会向hit-test view发送 touchesCancelled:withEvent:消息来取消hit-test view对此触摸序列的处理,这样只有Gesture Recognizers能响应此触摸序列,hit-test view不再响应。如果为NO,则不发送touchesCancelled:withEvent:消息给hit-test view,这样会使Gesture Recognizers和hit-test view同时响应触摸序列。
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
tapGesture.delegate = self;
tapGesture.cancelsTouchesInView = NO;
[self addGestureRecognizer:tapGesture];
tapGesture.cancelsTouchesInView直接设置NO就好
网友评论