UIGestureRecognizerDelegate有两个没公开的函数,只要重载了就会被调用。
即所有的UIGestureRecognizer子类.delegate = someInstance; 经过set以后,只要这个delegate实例里有这两个函数,就会被调用进入。经过验证,这两个api是可以通过apple审查上app store的。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer canBePreventedByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { BOOL result = YES; // something you do return result; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer canPreventGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { BOOL result = YES; // something you do return result; }
实际上UIGestureRecognizerDelegate这个Protocol有7个函数,公开的只有3个,我目前发现UIWebView系统有使用5个。
@protocol UIGestureRecognizerDelegatePrivate <UIGestureRecognizerDelegate> @optional - (BOOL)_gestureRecognizer:(id)arg1 canBePreventedByGestureRecognizer:(id)arg2; - (BOOL)_gestureRecognizer:(id)arg1 canPreventGestureRecognizer:(id)arg2; - (BOOL)_gestureRecognizer:(id)arg1 shouldReceiveTouch:(id)arg2; - (BOOL)_gestureRecognizer:(id)arg1 shouldRecognizeSimultaneouslyWithGestureRecognizer:(id)arg2; - (BOOL)_gestureRecognizerShouldBegin:(id)arg1; - (BOOL)_gestureRecognizer:(id)arg1 shouldBeRequiredToFailByGestureRecognizer:(id)arg2; - (BOOL)_gestureRecognizer:(id)arg1 shouldRequireFailureOfGestureRecognizer:(id)arg2; @end
使用场景:
一个view上以及其superview和subviews上有很多个gestureRecognizers时,可以使用这两个函数做手势间的互斥。如函数名那般顾名思义,返回值为第一个手势能否阻止或被阻止第二个手势的触发。
系统使用示例:
UIWebView的非公开子view,UIWebDocumentView有使用到,经反汇编,其部分实现为:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer canBePreventedByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { BOOL result = NO; // 有部分代码省略 if (_longPressGestureRecognizer != gestureRecognizer && _highlightLongPressGestureRecognizer != gestureRecognizer) result = YES; return result; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer canPreventGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { BOOL result = YES; // 有部分代码省略 if (_highlightLongPressGestureRecognizer == gestureRecognizer || _longPressGestureRecognizer == gestureRecognizer) { Class cls = NSClassFromString(@"UIScrollViewPanGestureRecognizer"); result = [otherGestureRecognizer isKindOfClass:cls] == NO; } return result; }