iOS的touchEvent的消费过程

移动开发 iOS
本文重点介绍了touch events用户触摸屏幕时触发事件的消费过程。内容比较少,但实践起来需要多多练习房能找到要领。

在iOS系统中,一共有三种类型的Event, 分别是touch events, motion events和remote-control events。touch events是用户触摸屏幕时触发的事件,motion events是用户移动设备时触发的事件,remote-control events是用户通过配件触发的事件(如耳机等线控或锁频时线控等)。我们用的最多,交互最多的当然就是touch events了。

阅读开发文档Event Handling Guide for iOS这一章,知道产生touches时,系统会将一系列的touches打包到UIEvent里,该UIEvent存储在当前应用event queue里,然后dispatch queue里面的事件给当前的key window, window通过hitTest: withEvent方法找到能够接受该event的view。在最顶层次的View里调用hitTest: withEvent,然后该方法再调用自身的pointInside: withEvent方法来判断需不需要处理,如果pointInside: withEvent返回YES,则hitTest: withEvent遍历所有subview,返回能够消费这个event的view,否则的话自身处理。如果pointInside: withEvent返回NO,则沿着responder chain向super view或者view controller传递event。如果view能够消费touch event,则系统会向该responder 发送touchesBegan: touchesMoved: touchesCancelled: touchesEnded:等消息,然后来处理产生的touch events。touch的生命周期是跟它的hitTestView绑定的,就是从hitTestView接受该touch开始直到该touch消失都跟该hitTestView绑定。

所以我们需要自行处理touch event时,需要实现touchesBegan: touchesMoved: touchesCancelled: touchesEnded:等方法,如果不需要自行处理,或者是同时保留系统处理,则我们只需调用如上方法的系统实现即super里面的方法即可。我们需 要拦截touch event时,在pointInside: withEvent里面返回YES,但不做处理可达到拦截的效果。需要注意的是,我们永远都不应该retain UIEvent,UIEvent是存储在event queue里面的,会循环利用,如果非得暂存某些状态,请将UIEvent的proterties拷贝然后使用NSDictionary等存储。

责任编辑:闫佳明 来源: cnblogs
相关推荐

2017-04-11 08:36:09

iOS编译应用

2013-05-14 11:01:34

AIR AndroidTouchEvent类

2013-05-14 11:04:58

AIR AndroidTouchEvent类

2022-06-01 06:17:42

微服务Kafka

2012-10-30 09:31:18

2016-08-03 15:47:20

AndroidIOSApple

2010-07-15 12:38:14

SQL Server存

2022-05-09 11:15:05

RocketMQPULL 模式PUSH 模式

2015-11-03 08:12:44

2013-04-03 10:42:46

iOS开发调试运行代码

2011-03-22 10:10:21

Nagios配置

2010-07-27 16:14:50

2022-08-09 08:18:19

RocketMQpush消费

2011-07-28 15:47:20

IOS 程序 测试

2014-12-10 21:50:44

AdMaster

2023-05-16 08:31:09

BrokerReef版本

2020-06-08 13:36:17

戴尔

2023-12-25 19:28:59

RocketMQ大数据

2021-12-27 08:22:18

Kafka消费模型

2023-11-07 12:09:44

TopicKafka
点赞
收藏

51CTO技术栈公众号