IOS事件传递之hitTest:withEvent - IOS开发...
来源:互联网 发布:php curl get 数据 编辑:程序博客网 时间:2024/06/03 20:38
原文 http://www.cnblogs.com/ioooooos/p/3617213.html
一、系统是怎么找到第一响应者的? --只通过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方法
- (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来实现:
-(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. 官方文档
2. hitTest:withEvent
3. hitTest:withEvent方法流程
- IOS事件传递之hitTest:withEvent - IOS开发...
- IOS事件传递之hitTest:withEvent
- iOS事件传递之hitTest:withEvent
- IOS事件传递之hitTest:withEvent
- 深入理解iOS API系列(三)UiView事件传递相关函数:pointInside:withEvent:和hittest:withevent:
- iOS hitTest:withEvent:
- iOS UIView事件隔层传递 pointInside:withEvent:
- 小胖说事45-----iOS hitTest:withEvent:方法流程
- 事件拦截器hitTest:withEvent
- iOS-hitTest:withEvent:在高德地图中的应用
- iOS-hitTest:withEvent与自定义hit-testing规则
- iOS开发-事件传递与响应者链中的hitTest方法和pointInside方法
- 事件传递之hitTest方法
- iOS 之hitTest学习
- hitTest:withEvent:
- hitTest:withEvent:
- hitTest:withEvent:
- ios hitTest
- 数据库事务隔离级别
- appium之java API(2) -TouchAction
- 利用Jsoup爬取天猫列表页数据
- spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(二)
- Address already in use: JVM_Bind
- IOS事件传递之hitTest:withEvent - IOS开发...
- Android Studio 的一些简单配置
- 外连接和内连接
- 项目总结——MongoDB入门
- 2014.1 网易游戏平台开发实习面试总结
- ASP.NET使用Cookie简单实现记住登陆状态功能
- 技术更新如此快的情况下,给程序员的一点建议
- 94. Binary Tree Inorder Traversal Leetcode Python
- Servlet监听器定时监听在线用户生成列表