iOS开发之-Event Handling

About Events in iOS

1. Events are objects sent to an application to inform it of user actions. The UIKit and Core Motion frameworks are responsible for event propagation and delivery in iOS.



An Application Receives Multitouch Events When Users Touch its Views

The application sends these events (as UIEvent objects) to the view on which the touches occurred. That view typically analyzes the touches represented by each event object and responds in an appropriate manner.


An Application Receives Motion Events When Users Move the Device

Motion events come in different forms, and you can handle them using different frameworks.


Remote-Control Events Are Sent When Users Manipulate Multimedia Controls


Event Types and Delivery

1. Each of these hardware systems, as they detect touches, device movements, and location changes, produce raw data that is passed to system frameworks. The frameworks package the data and deliver them as events to an application for processing.

UIKit Event Objects and Types

1. An event is an object that represents a user action detected by hardware on the device and conveyed to iOS


2. A UIEvent object may encapsulate state related to the user event, such as the associated touches. It also records the moment the event was generated.


3. UIKit currently recognizes three types of events

typedef enum {
    UIEventTypeTouches,
    UIEventTypeMotion,
    UIEventTypeRemoteControl,
} UIEventType;
 
typedef enum {
    UIEventSubtypeNone                              = 0,
 
    UIEventSubtypeMotionShake                       = 1,
 
    UIEventSubtypeRemoteControlPlay                 = 100,
    UIEventSubtypeRemoteControlPause                = 101,
    UIEventSubtypeRemoteControlStop                 = 102,
    UIEventSubtypeRemoteControlTogglePlayPause      = 103,
    UIEventSubtypeRemoteControlNextTrack            = 104,
    UIEventSubtypeRemoteControlPreviousTrack        = 105,
    UIEventSubtypeRemoteControlBeginSeekingBackward = 106,
    UIEventSubtypeRemoteControlEndSeekingBackward   = 107,
    UIEventSubtypeRemoteControlBeginSeekingForward  = 108,
    UIEventSubtypeRemoteControlEndSeekingForward    = 109,
} UIEventSubtype;

4. Each event has one of these event type and subtype constants associated with it, which you can access through the type and subtype properties of UIEvent. Touch events always have a subtype of UIEventSubtypeNone.

5. You should never retain a UIEvent object in your code. If you need to preserve the current state of an event object for later evaluation, you should copy and store those bits of state in an appropriate manner (using an instance variable or a dictionary object, for example).

Event Delivery

1. When users touch the screen of a device, iOS recognizes the set of touches and packages them in a UIEvent object that it places in the active application’s event queue. The singleton UIApplication object managing the application takes an event from the top of the queue and dispatches it for handling. Typically, it sends the event to the application’s key window and the window  sends the event to an initial object for handling. 

2. Touch events. 
The window object uses hit-testing and the responder chain to find the view to receive the touch event.That view becomes the hit-test view.

If the hit-test view cannot handle the event, the event travels up the responder chain until the system finds a view that can handle it. A touch object is associated with its hit-test view for its lifetime, even if the touch represented by the object subsequently moves outside the view. 

3. Motion and remote-control events. 
The window object sends each shaking-motion or remote-control event to the first responder for handling.
Although the hit-test view and the first responder are often the same view object, they do not have to be the same.

Responder Objects and the Responder Chain

1. A responder object is an object that can respond to events and handle them. UIResponder is the base class for all responder objects.


2. The first responder is the responder object in an application (usually a UIView object) that is designated to be the first recipient of events other than touch events. To receive these messages, the responder object must implement canBecomeFirstResponder to return YES; it must also receive a becomeFirstResponder message. 


3. The first responder is the first view in a window to receive the following type of events and messages:

Motion events
Remote-control events
Action messages—sent when the user manipulates a control (such as a button or slider) and no target is specified for the action message

4. The first responder also plays a role in text editing. A text view or text field that is the focus of editing is made the first responder, which causes the virtual keyboard to appear.


5. If the first responder or the hit-test view doesn’t handle an event, UIKit may pass the event (via message) to the next responder in the responder chain to see if it can handle it.



6. The responder chain is a linked series of responder objects along which an event, action message, or editing-menu message is passed. 


7. When the system delivers a touch event, it first sends it to a specific view. For touch events, that view is the one returned by hitTest:withEvent:; for “shaking”-motion events, remote-control events, action messages, and editing-menu messages, that view is the first responder. If the initial view doesn’t handle the event, it travels up the responder chain along a particular path:

8. The hit-test view or first responder passes the event or message to its view controller if it has one; if the view doesn’t have a view controller, it passes the event or message to its superview.


Motion Event Types

关于加速计和陀螺仪的event

Multitouch Events



1. Touches, which are represented by UITouch objects, have both temporal and spatial aspects. 
The temporal aspect, called a phase, indicates when a touch has just begun, whether it is moving or stationary, and when it ends—that is, when the finger is lifted from the screen.
The spatial aspect of touches concerns their association with the object in which they occur as well as their location in it. 


2. An event object contains all touch objects for the current multitouch sequence and can provide touch objects specific to a view or window. A touch object is persistent for a given finger during a sequence, and UIKit mutates it as it tracks the finger throughout it. The touch attributes that change are the phase of the touch, its location in a view, its previous location, and its timestamp. 

Regulating Touch Event Delivery

1. Turning off delivery of touch events. By default, a view receives touch events, but you can set its userInteractionEnabled property to NO to turn off delivery of touch events. A view also does not receive these events if it’s hidden or if it’s transparent.

2. Turning off delivery of touch events for a period. An application can call the UIApplication method beginIgnoringInteractionEvents and later call the endIgnoringInteractionEvents method. You sometimes want to turn off event delivery while your code is performing animations.

3. Turning on delivery of multiple touches. By default, a view ignores all but the first touch during a multitouch sequence. If you want the view to handle multiple touches you must enable this capability for the view. You do this programmatically by setting the multipleTouchEnabled property of your view to YES

4. Restricting event delivery to a single view. By default, a view’s exclusiveTouch property is set to NO, which means that this view does not block other views in a window from receiving touches. If you set the property to YES, you mark the view so that, if it is tracking touches, it is the only view in the window that is tracking touches.

Handling Multitouch Events

1. UIResponder Methods:
- (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

The first is a set of UITouch objects that represent new or changed touches for the given phase. The second parameter is a UIEvent object representing this particular event. 

2. During a multitouch sequence, the application dispatches a series of event messages to the target responder.

3. An important UITouch method is locationInView:, A parallel method tells you the previous location of the touch (previousLocationInView:). Properties of the UITouch instance tell you how many taps have been made (tapCount), when the touch was created or last mutated (timestamp), and what phase it is in (phase).



4. You can send methods like:
allTouches:
touchesForWindow:
touchesForView:

5. If your custom responder class is a subclass of UIView or UIViewController, you should implement all of the methods described in “The Event-Handling Methods.










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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值