hitTest的作用:當在一個view上添加一個屏蔽罩,但又不影響對下面view的操作,也就是可以透過屏蔽罩對下面的view進行操作,這個函數就很好用了。
hitTest的用法:將下面的函數添加到UIView的子類中,也就是屏蔽罩類中即可。
-(id)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView *hitView = [super hitTest:point withEvent:event];
if (hitView == self)
{
return nil;
}
else
{
return hitView;
}
}
轉自:http://blog.sina.com.cn/s/blog_446da0320100yw9u.html
另外轉一個相關的文章:
UiView touch事件傳遞- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{} 這個函數的用處是判斷當前的點擊或者觸摸事件的點是否在當前的view中。 它被hitTest:withEvent:調用,通過對每個子視圖調用pointInside:withEvent:決定最終哪個視圖來響應此事件。如果 PointInside:withEvent:返回YES,然後子視圖的繼承樹就會被遍曆(遍曆順序中最先響應的為:與使用者最接近的那個視圖。 it starts from the top-level subview),即子視圖的子視圖繼續調用遞迴這個函數,直到找到可以響應的子視圖(這個子視圖的hitTest:withEvent:會返回self,而不是nil);否則,視圖的繼承樹就會被忽略。 當我們需要重寫某個UIView的繼承類UIViewInherit的時候,如果需要重寫hitTest:withEvent:方法,就會出現是否調用[super hitTest:withEvent:]方法的疑問?究竟是否需要都是看具體需求,這裡只是說明調與不調的效果。 如果不調用,那麼重寫的方法hitTest:withEvent:只會調用重寫後的代碼,根據所重寫的代碼返回self或nil,如果返回self那麼你的這個UIViewInherit類會接受你的按鍵,然後調用touches系列方法;否則返回nil那麼傳遞給UIViewInherit類的按鍵到此為止,它不接受它的父view給它的按鍵,即不會調用touches系列方法。這時,PointInside:withEvent:幾乎沒有作用。 如果調用,那麼[super hitTest:withEvent:]方法首先是根據PointInside:withEvent:的傳回值決定是否遞迴調用所有子View的hitTest:withEvent:方法。對於子View的hitTest:withEvent:方法調用也是一樣的過程,這樣一直遞迴下去,直到最先找到的某個遞迴層次上的子View的hitTest:withEvent:方法返回非nil,這時候,調用即結束,最終會調用這個子View的touches系列方法。 如果我們不想讓某個視圖響應事件,只需要重載 PointInside:withEvent:方法,讓此方法返回NO就行了。不過從這裡,還是不能瞭解到hitTest:WithEvent的方法的用途。http://blog.sina.com.cn/s/blog_87bed3110100t5cf.htmlhttp://blog.csdn.net/iefreer/article/details/4754482hitTest:withEvent:調用過程The implementation of hitTest:withEvent: in UIResponder does the following:It calls pointInside:withEvent: of selfIf the return is NO, hitTest:withEvent: returns nil. the end of the story.If the return is YES, it sends hitTest:withEvent: messages to its subviews. it starts from the top-level subview, and continues to other views until a subview returns a non-nil object, or all subviews receive the message.If a subview returns a non-nil object in the first time, the first hitTest:withEvent: returns that object. the end of the story.If no subview returns a non-nil object, the first hitTest:withEvent: returns selfThis process repeats recursively, so normally the leaf view of the view hierarchy is returned eventually.However, you might override hitTest:withEvent to do something differently. In many cases, overriding pointInside:withEvent: is simpler and still provides enough options to tweak event handling in your application.