UIEvent - 1

学习UIRespnder这个非常重要的类离不开UIEvent这个类,而在网上找不到关于UIEvent这个类很好的总结很阐述,只能自己看看官方文档自己总结一番了。

一个UIEvent事件对象通常代表了一个iOS的事件,通常有3种事件类型,由这个枚举类型指出:

typedef NS_ENUM(NSInteger, UIEventType) {
    UIEventTypeTouches,
    UIEventTypeMotion,
    UIEventTypeRemoteControl,
};

根据苹果对它们的描述:


1.触碰事件 : UIEventTypeTouches

这个事件代表着对屏幕的一切点击操作。



2.运动事件 : UIEventTypeMotion

这个事件代表着设备的运动,例如当用户摇设备,非常好理解,微信的摇一摇



3.远程控制事件 : UIEventTypeRemoteControl

这个事件代表着远程控制事件,远程控制事件起源于例如从耳机或者外部部件接收用于控制设备上的多媒体的目的命令,这个也非常好理解,例如有些带有调节音量和话筒功能的耳机,还更多风靡于老少男女的自拍杠。

(转自Reference 1)


-----

A touch type of event object contains one or more touches (that is, finger gestures on the screen) that have some relation to the event. A touch is represented by a UITouch object. When a touch event occurs, the system routes it to the appropriate responder and passes in the UIEvent object in a message invoking a UIResponder method such as touchesBegan:withEvent:. The responder can then evaluate the touches for the event or for a particular phase of the event and handle them appropriately. The methods of UIEvent allow you to obtain all touches for the event (allTouches) or only those for a given view or window (touchesForView: or touchesForWindow:). It can also distinguish an event object from objects representing other events by querying an object for the time of its creation (timestamp).

一个触碰类型的事件对象包含了一个或者多个触碰(也就是,在屏幕上的手指手势)有着一些与事件的关系。一个触碰是用一个UITouch对象来代表的。当一个触碰事件发生,系统会循路至合适的响应链并且用一个消息调用一个UIResponder方法例如touchesBegan:withEvent.将它传递到UIEvent对象。这个响应链会接着评估触碰的事件或者对于事件的特殊解析并且合适地处理它们。UIEvent的方法将会允许你去获得所有触碰事件或者是仅仅那些对于给定的视图或者窗口(用touchesForView:or touchesForWindow:)。它同时会区分事件对象从代表其他事件的对象通过查询一个对象在对象的创建时候(用时间戳)

这里提到了触碰事件的方法,我们来看看UIEvent对于触碰到底有哪些方法:

- (NSSet *)allTouches;
- (NSSet *)touchesForWindow:(UIWindow *)window;
- (NSSet *)touchesForView:(UIView *)view;
- (NSSet *)touchesForGestureRecognizer:(UIGestureRecognizer *)gesture NS_AVAILABLE_IOS(3_2);

分别有所有触碰事件,对于窗口类型的触碰事件,对于视图类型的触碰事件,对于手势对象的触碰事件,注意的是,都是返回的为NSSet类型,代表的是集合的一类对象


-----

A UIEvent object representing a touch event is persistent throughout a multi-touch sequence; UIKit reuses the same UIEvent instance for every event delivered to the application. You should never retain an event object or any object returned from an event object. If you need to keep information from an event around from one phase to another, you should copy that information from the UITouch or UIEvent object.

一个UIEvent对象代表着一个触碰事件持续整个“多点触碰”队列;UIKit框架重用了和UIEvent一样的例子对于任何传递给应用的事件。你应该不要持续整个事件对象或者任意从事件对象返回的对象。如果你需要保存一个事件周围的信息从一个阶段到另外一个阶段,你应该用copy处理这些信息从UITouch或者UIEvent对象

第一次遇见这种情况:不建议用retain,而是用copy,说明对于内存管理的体系知识还是不熟


-----

You can obtain event types and subtypes from the type and subtype properties. UIEvent defines event types for touch, motion, and remote-control events. It also defines a motion subtype for "shake” events and a series of subtype constants for remote-control events, such as “play” and “previous track.” The first responder or any responder in the responder chain implements the motion-related methods of UIResponder (such as motionBegan:withEvent:) to handle shaking-motion events. To handle remote-control events, a responder object must implement the remoteControlReceivedWithEvent: method of UIResponder.

你可以获得一个事件类型或者是 “子类型” 从 type 和 subtype 属性。UIEvent为touch,motion和remote-control事件定义了事件类型。它同样也为”摇动“事件和一系列远程控制事件的子类型约束定义了一个运动子类型,例如 ”play“ 和 ”previous track“ 。在响应链中的第一个响应链或任意响应者实现了 与运动相关的 方法在UIResponder类中(例如motionBegan:withEvent:)来摇动运动事件。为了处理远程控制事件,一个响应对象必须实现remoteControlReceivedWithEvent:方法
这一节提到几点:一、 subtype:type属性就是三大类型,而subtype就是一些运动和远程控制的类型的枚举集合,甚至有一个None不操作。二、motion-related methods of UIResponder,如下:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

三、为了实现远程控制事件必须实现的一个方法:

- (void)remoteControlReceivedWithEvent:(UIEvent *)event NS_AVAILABLE_IOS(4_0);


-----

The touchesForGestureRecognizer: method, which was introduced in iOS 3.2, allows you to query a gesture-recognizer object (an instance of a subclass of UIGestureRecognizer) for the touches it is currently handling.







下一节会更加详细地讲解iOS的事件体系知识

参考:

Reference 1 :   http://www.cnblogs.com/kenshincui/p/3950646.html#touch

Reference 2 :   http://www.cnblogs.com/syxchina/archive/2012/10/14/2723541.html

Reference 3 :   http://blog.csdn.net/z888c/article/details/6525822





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值