深入理解iOS API系列(三)UiView事件传递相关函数:pointInside:withEvent:和hittest:withevent:

关于此API的说法网上总说纷纭,可以看看UIView的剖析(管蕾的《iOS8开发指南》第十六章抄袭来源)、hitTest:withEvent:方法流程为易于理解,模拟UIView的hitTest:方法和pointInside:方法的实现ios的hitTest方法以及不规则区域内触摸事件处理方法iOS触摸事件处理等文。

本文采信http://blog.chinaunix.net/uid-26466552-id-3031245.html,原文如下:

- (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.

--------------------------------结束----------------------------------


- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event在各类视图的父类UIView中被实现,可能的实现为:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
    return CGRectContainsPoint(self.bounds, point);
}
在UIView中它应当为hittest:withevent:所调用。虽然《模拟UIView的hitTest:方法和pointInside:方法的实现》一文中并未正确实现pointInside:withEvent:方法,但是整个流程还是比较正确的。首先触摸事件到达UIApplication,再到UIWindow。假定我们实现了自己MyView作为window根视图控制器的view,那么UIWindow会调用MyView的hittest:withevent:看看MyView是否响应触摸事件。此时如果你不覆盖hittest:withevent:,仅自动调用UIResponder的实现便可正常执行。但是你有特殊需求,要自己实现MyView的hittest:withevent:,那么应该如何实现呢?

 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
         // do something
         return [super hitTest:point withEvent:event];
}

而在super的实现类似《模拟UIView的hitTest:方法和pointInside:方法的实现》,它会先根据MyView(如果实现,否则就是UIResponder的默认实现)的pointInside:withEvent:决定是否向subviews发送hittest:withevent:。hittest:withevent:会返回触摸最后应真正响应的视图。每一分支hittest如果返回nil就不再继续hittest。如果所有分支返回nil,则第一个hittest返回MyView本身。

当然你也可以不调用super的,自己实现它。

- (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 <span>reverseObjectEnumerator</span>]) {
            //注意,这里有坐标转换,将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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值