[IOS]hitTest的做用與用法

hitTest的做用:當在一個view上添加一個屏蔽罩,但又不影響對下面view的操做,也就是能夠透過屏蔽罩對下面的view進行操做,這個函數就很好用了。 html


 

hitTest的用法:將下面的函數添加到UIView的子類中,也就是屏蔽罩類中便可。 app

 

-(id)hitTest:(CGPoint)point withEvent:(UIEvent *)event  ide

{ 函數

    UIView *hitView = [super hitTest:point withEvent:event]; spa

    if (hitView == self) .net

    { orm

        return nil; htm

    } blog

    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.html
http://blog.csdn.net/iefreer/article/details/4754482

hitTest:withEvent:調用過程

The implementation of hitTest:withEvent: in UIResponder does the following:

It calls pointInside:withEvent: of self
If 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 self
This 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.
相關文章
相關標籤/搜索