[IOS]hitTest的作用与用法【转】

本文详细解析了iOS开发中UIView的hitTest:withEvent:方法的使用技巧及原理,阐述了如何通过重写该方法实现特定的触摸事件传递逻辑,并对比了不同实现方式的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

hitTest的作用:当在一个view上添加一个屏蔽罩,但又不影响对下面view的操作,也就是可以透过屏蔽罩对下面的view进行操作,这个函数就很好用了。


<wbr><wbr></wbr></wbr>

hitTest的用法:将下面的函数添加到UIView的子类中,也就是屏蔽罩类中即可。

<wbr><wbr></wbr></wbr>

-(id)hitTest:(CGPoint)point withEvent:(UIEvent<wbr>*)event<wbr><wbr></wbr></wbr></wbr>

{

<wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:24px; color:rgb(126,34,172)">UIView</span><wbr>*hitView = [<span style="word-wrap:normal; word-break:normal; line-height:24px; color:rgb(208,0,162)">super</span><wbr><span style="word-wrap:normal; word-break:normal; line-height:24px; color:rgb(73,0,132)">hitTest</span>:point<wbr><span style="word-wrap:normal; word-break:normal; line-height:24px; color:rgb(73,0,132)">withEvent</span>:event];</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:24px; color:rgb(208,0,162)">if</span><wbr>(hitView ==<wbr><span style="word-wrap:normal; word-break:normal; line-height:24px; color:rgb(208,0,162)">self</span>)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr>{</wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:24px; color:rgb(208,0,162)">return</span><wbr><span style="word-wrap:normal; word-break:normal; line-height:24px; color:rgb(208,0,162)">nil</span>;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:24px; color:rgb(208,0,162)">else</span><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr>{</wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:24px; color:rgb(208,0,162)">return</span><wbr>hitView;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr>

}


转自:http://blog.sina.com.cn/s/blog_446da0320100yw9u.html


另外转一个相关的文章:

UiViewtouch事件传递

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
}

<wbr><wbr>这个函数的用处是判断当前的点击或者触摸事件的点是否在当前的view中。</wbr></wbr>
<wbr><wbr>它被hitTest:withEvent:调用,通过对每个子视图调用pointInside:withEvent:决定最终哪个视图来响应此事件。如果 PointInside:withEvent:返回YES,然后子视图的继承树就会被遍历(遍历顺序中最先响应的为:与用户最接近的那个视图。 it starts from the top-levelsubview),即子视图的子视图继续调用递归这个函数,直到找到可以响应的子视图(这个子视图的hitTest:withEvent:会返回self,而不是nil);否则,视图的继承树就会被忽略。</wbr></wbr>

<wbr><wbr>当我们需要重写某个UIView的继承类UIViewInherit的时候,如果需要重写hitTest:withEvent:方法,就会出现是否调用[super hitTest:withEvent:]方法的疑问?究竟是否需要都是看具体需求,这里只是说明调与不调的效果。</wbr></wbr>
<wbr><wbr>如果不调用,那么重写的方法hitTest:withEvent:只会调用重写后的代码,根据所重写的代码返回self或nil,如果返回self那么你的这个UIViewInherit类会接受你的按键,然后调用touches系列方法;否则返回nil那么传递给UIViewInherit类的按键到此为止,它不接受它的父view给它的按键,即不会调用touches系列方法。这时,PointInside:withEvent:几乎没有作用。</wbr></wbr>
<wbr><wbr>如果调用,那么[super hitTest:withEvent:]方法首先是根据PointInside:withEvent:的返回值决定是否递归调用所有子View的hitTest:withEvent:方法。对于子View的hitTest:withEvent:方法调用也是一样的过程,这样一直递归下去,直到最先找到的某个递归层次上的子View的hitTest:withEvent:方法返回非nil,这时候,调用即结束,最终会调用这个子View的touches系列方法。</wbr></wbr>
<wbr></wbr>
<wbr><wbr><wbr>如果我们不想让某个视图响应事件,只需要重载 PointInside:withEvent:方法,让此方法返回NO就行了。不过从这里,还是不能了解到hitTest:WithEvent的方法的用途。</wbr></wbr></wbr>

http://blog.sina.com.cn/s/blog_87bed3110100t5cf.html
http://blog.youkuaiyun.com/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 returnsa non-nil object in thefirst time, the first hitTest:withEvent: returns that object. the end of the story.
If no subview returns anon-nilobject, the first hitTest:withEvent: returns self
This process repeats recursively, so normally the leafview of the view hierarchy is returned eventually.

However, you might override hitTest:withEvent to do something differently. Inmany cases, overriding pointInside:withEvent: is simpler and still provides enough options to tweak event handling in your application.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值