[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.
相关文章
相关标签/搜索