iOS ---UIView的hitTest:方法和pointInside:


先是基本概念分析

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)eventhtml

{app

}ide


   這個函數的用處是判斷當前的點擊或者觸摸事件的點是否在當前的view中。函數

   它被hitTest:withEvent:調用,經過對每一個子視圖調用pointInside:withEvent:決定最終哪一個視圖來響應此事件。若是 PointInside:withEvent:返回YES,而後子視圖的繼承樹就會被遍歷(遍歷順序中最早響應的爲:與用戶最接近的那個視圖。 it starts from the top-level subview),即子視圖的子視圖繼續調用遞歸這個函數,直到找到能夠響應的子視圖(這個子視圖的hitTest:withEvent:會返回self,而不是nil);不然,視圖的繼承樹就會被忽略。spa


    當咱們須要重寫某個UIView的繼承類UIViewInherit的時候,若是須要重寫hitTest:withEvent:方法,就會出現是否調用[super hitTest:withEvent:]方法的疑問?到底是否須要都是看具體需求,這裏只是說明調與不調的效果。.net

    若是不調用,那麼重寫的方法hitTest:withEvent:只會調用重寫後的代碼,根據所重寫的代碼返回self或nil,若是返回self那麼你的這個UIViewInherit類會接受你的按鍵,而後調用touches系列方法;不然返回nil那麼傳遞給UIViewInherit類的按鍵到此爲止,它不接受它的父view給它的按鍵,即不會調用touches系列方法。這時,PointInside:withEvent:幾乎沒有做用。code

    若是調用,那麼[super hitTest:withEvent:]方法首先是根據PointInside:withEvent:的返回值決定是否遞歸調用全部子View的hitTest:withEvent:方法。對於子View的hitTest:withEvent:方法調用也是同樣的過程,這樣一直遞歸下去,直到最早找到的某個遞歸層次上的子View的hitTest:withEvent:方法返回非nil,這時候,調用即結束,最終會調用這個子View的touches系列方法。orm

 

     若是咱們不想讓某個視圖響應事件,只須要重載 PointInside:withEvent:方法,讓此方法返回NO就好了。不過從這裏,仍是不能瞭解到hitTest:WithEvent的方法的用途。htm


http://blog.sina.com.cn/s/blog_87bed3110100t5cf.htmlblog

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.



爲易於理解,模擬UIView的hitTest:方法和pointInside:方法的實現

對於UIView 的兩個方法的講解:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
網上對這兩個方法的講解不少,可是大部分是純文字的描述,我再也不贅述,須要能夠本身百度「UIView hitTest」等等。

我如今根據個人理解,把這兩個方法的源碼實現模擬出來。
注意:這裏只是模擬,是爲了讓你更容易理解而已,距離真實的源碼還有很大的差距,
好比裏面的event我根本沒用到。

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
 

  UIView *touchView = self;
   if ([self pointInside:point withEvent:event] &&

    (!self.hidden) && 

    self.userInteractionEnabled &&

    (self.alpha >= 0.01f)) {


       for (UIView *subView in self.subviews) {
           //注意,這裏有座標轉換,將point點轉換到subview中,好好理解下
           CGPoint subPoint = CGPointMake(point.x - subView.frame.origin.x,

                      point.y - subView.frame.origin.y);
           UIView *subTouchView = [subView hitTest:subPoint withEvent:event];
           if (subTouchView) {
               //找到touch事件對應的view,中止遍歷
               touchView = subTouchView;
               break;
           }
       }
   }else{
       //此點不在該View中,那麼連遍歷也省了,直接返回nil
       touchView = nil;
   }
   
   return touchView;
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
   return CGRectContainsPoint(self.bounds, point);
}

簡單示例

目前最簡單和直接的隱藏鍵盤的方法,經過覆蓋hitTest:withEvent:方法並調用endEditing:方法來隱藏鍵盤。

先上代碼

?

1
2
3
4
5
6
7
8
9
@implementation ALXBackgroundView
 
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
     UIView *view = [super hitTest:point withEvent:event];
     [view endEditing:YES];   //隱藏鍵盤
     return  view;
}
 
相關文章
相關標籤/搜索