hitTest:withEvent應(yīng)用:(hitTest可實(shí)現(xiàn)擊穿點(diǎn)擊)
1)
父視圖中有布局重疊的且都可響應(yīng)用戶操作的對(duì)象,如:ScrollView and
Button,如果Button在ScrollView下面,正常情況下Button是不會(huì)成為第一響應(yīng)者的,如果想讓Button可以響應(yīng)在其布局內(nèi)的
觸摸事件,可以在Button和ScrollView的父View中重寫(xiě)hitTest:withEvent方法
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
CGPoint hitPoint = [_testButton convertPoint:point fromView:self];
if([_testButton pointInside:hitPoint withEvent:event])
return_testButton;
return[superhitTest:point withEvent:event];
}//_testButton是指定響應(yīng)對(duì)象的 弱 引用
2)UIView的子類不響應(yīng)觸摸事件,但其子View可以響應(yīng)。通過(guò)設(shè)置userInteractionEnabled=NO,可以使UIView子類不響應(yīng)觸摸事件,但其會(huì)挾持子View,原因見(jiàn)3)
這時(shí),可以通過(guò)重寫(xiě)hitTest:withEvent來(lái)實(shí)現(xiàn):
-(id)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
idhitView = [superhitTest:point withEvent:event];
if(hitView ==self)returnnil;
elsereturnhitView;
}
3)
userInteractionEnabled =
NO的作用:使當(dāng)前的hitTest:withEvent返回nil,其它的類似屬性還有:Hidden=Y(jié)ES,alpha<0.01,
(UIControl中Enabled=NO??),事件發(fā)生的點(diǎn)在子View的幾何范圍內(nèi),卻超過(guò)了父View的幾何范圍
(clipsToBounds=NO時(shí)可出現(xiàn)此種情況)