iOS事件传递之hitTest:withEvent
2015-03-27 18:05
375 查看
一、系统是怎么找到接收触摸事件发生的视图的? --只通过UIView及其子类查找
0 调用根视图的hitTtest:withEvent,其的执行过程如下:
Ie calls pointInside:withEvent:of self
If the return is NO,
If the return is YES, it sends
If a subview returns a non-
If no subview returns a non-
参考:这里
二 、触摸事件是如何传递的?
![](http://img.blog.csdn.net/20130707181937406?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd3p6dmljdG9yeV90anNk/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
三、hitTest:withEvent应用:
1)父视图中有布局重叠的且都可响应用户操作的对象,如:ScrollView and Button,如果Button在ScrollView下面,正常情况下Button是不会成为第一响应者的,如果想让Button可以响应在其布局内的触摸事件,可以在Button和ScrollView的父View中重写hitTest:withEvent方法
参考:这里
2)UIView的子类不响应触摸事件,但其子View可以响应。通过设置userInteractionEnabled=NO,可以使UIView子类不响应触摸事件,但其会挟持子View,原因见3)
这时,可以通过重写hitTest:withEvent来实现:
参考:这里
3) userInteractionEnabled = NO的作用:使当前的hitTest:withEvent返回nil,其它的类似属性还有:Hidden=YES,alpha<0.01,(UIControl中Enabled=NO??),事件发生的点在子View的几何范围内,却超过了父View的几何范围(clipsToBounds=NO时可出现此种情况)
1.官方文档
2.hitTest:withEvent
3.hitTest:withEvent方法流程
0 调用根视图的hitTtest:withEvent,其的执行过程如下:
Ie 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-
nilobject, or all subviews receive the message.
If a subview returns a non-
nilobject in the first time, the first
hitTest:withEvent:returns that object. the end of the story.
If no subview returns a non-
nilobject, the first
hitTest:withEvent:returns
self
参考:这里
二 、触摸事件是如何传递的?
三、hitTest:withEvent应用:
1)父视图中有布局重叠的且都可响应用户操作的对象,如:ScrollView and Button,如果Button在ScrollView下面,正常情况下Button是不会成为第一响应者的,如果想让Button可以响应在其布局内的触摸事件,可以在Button和ScrollView的父View中重写hitTest:withEvent方法
2)UIView的子类不响应触摸事件,但其子View可以响应。通过设置userInteractionEnabled=NO,可以使UIView子类不响应触摸事件,但其会挟持子View,原因见3)
这时,可以通过重写hitTest:withEvent来实现:
3) userInteractionEnabled = NO的作用:使当前的hitTest:withEvent返回nil,其它的类似属性还有:Hidden=YES,alpha<0.01,(UIControl中Enabled=NO??),事件发生的点在子View的几何范围内,却超过了父View的几何范围(clipsToBounds=NO时可出现此种情况)
1.官方文档
2.hitTest:withEvent
3.hitTest:withEvent方法流程
相关文章推荐
- IOS事件传递之hitTest:withEvent
- IOS事件传递之hitTest:withEvent
- 深入理解iOS API系列(三)UiView事件传递相关函数:pointInside:withEvent:和hittest:withevent:
- IOS事件传递之hitTest:withEvent - IOS开发...
- iOS hitTest 处理UIView事件传递
- iOS开发-事件传递与响应者链中的hitTest方法和pointInside方法
- ios事件传递机制
- IOS事件传递
- iOS事件传递机制(一)
- iOS事件传递机制(二)
- 『IOS』Responder Chain(ios事件传递)
- <iOS>响应事件传递, nextResponder研究
- IOS的事件传递
- 在iOS中限制事件传递
- iOS:事件处理机制(二)--事件传递,Responder Chain响应链
- <iOS>响应事件传递, nextResponder研究
- iOS 事件响应链机制 及 触摸事件(hitTest)
- iOS事件传递机制(一)
- IOS 响应事件传递nextResponder
- iOS 中 点击事件响应的方法hitTest与pointInside