IOS事件传递之hitTest:withEvent
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了IOS事件传递之hitTest:withEvent,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1804字,纯文字阅读大概需要3分钟。
内容图文
一、系统是怎么找到第一响应者的? --只通过UIView及其子类查找
0 调用根视图的hitTtest:withEvent,其的执行过程如下:
- Ie calls pointInside:withEvent:of self
- If the return is
NO,
hitTest:withEvent:
returnsnil
. 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 firsthitTest:withEvent:
returns that object. the end of the story. - If no subview returns a non-
nil
object, the firsthitTest:withEvent:
returnsself
参考:这里
二 、触摸事件是如何传递的?
三、hitTest:withEvent应用:
1)父视图中有布局重叠的且都可响应用户操作的对象,如:ScrollView and Button,如果Button在ScrollView下面,正常情况下Button是不会成为第一响应者的,如果想让Button可以响应在其布局内的触摸事件,可以在Button和ScrollView的父View中重写hitTest:withEvent方法
1
2
3
4
5
6
7
|
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
CGPoint hitPoint = [_testButton convertPoint:point fromView:
self
];
if
([_testButton pointInside:hitPoint withEvent:event])
return
_testButton;
return
[
super
hitTest:point withEvent:event];
}
//_testButton是指定响应对象的 弱 引用
|
参考:这里
2)UIView的子类不响应触摸事件,但其子View可以响应。通过设置userInteractionEnabled=NO,可以使UIView子类不响应触摸事件,但其会挟持子View,原因见3)
这时,可以通过重写hitTest:withEvent来实现:
1
2
3
4
5
|
-(
id
)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
id
hitView = [
super
hitTest:point withEvent:event];
if
(hitView ==
self
)
return
nil
;
else
return
hitView;
}
|
参考:这里
3) userInteractionEnabled = NO的作用:使当前的hitTest:withEvent返回nil,其它的类似属性还有:Hidden=YES,alpha<0.01,(UIControl中Enabled=NO??),事件发生的点在子View的几何范围内,却超过了父View的几何范围(clipsToBounds=NO时可出现此种情况)
参考:
1.官方文档
原文:http://www.cnblogs.com/ioooooos/p/3617213.html
内容总结
以上是互联网集市为您收集整理的IOS事件传递之hitTest:withEvent全部内容,希望文章能够帮你解决IOS事件传递之hitTest:withEvent所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。