Events in iOS, iOS events

Source: Internet
Author: User

Events in iOS, iOS events
Events in iOS

  • When a user uses an app, various events are generated.
  • Events in iOS can be divided into three types:
    --- Touch event ------- accelerator event ------ Remote Control event ---
Recipient
  • In iOS, not all objects can process events. Only objects that inherit UIResponder can receive and process events. We call it the "response object"
  • UIApplication, UIViewController, and UIView are all inherited from UIResponder. Therefore, they are all responder objects that can receive and process events.
UIResponder

UIResponder provides the following methods to handle events:

  • Touch event
- (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;
  • Accelerator event
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;
  • Remote Control event
- (void)remoteControlReceivedWithEvent:(UIEvent *)event;
Processing of touch events in UIView

UIView is a subclass of UIResponder. You can implement the following four methods to handle different touch events:
One or more fingers start to touch the view. The system automatically calls the following method of view.
-(Void) touchesBegan :( NSSet) Touches withEvent :( UIEvent) Event

When one or more fingers move on the view, the system automatically calls the following method of the view (this method will be continuously called as the fingers move)
-(Void) touchesMoved :( NSSet) Touches withEvent :( UIEvent) Event

If one or more fingers exit the view, the system automatically calls the following method of the view.
-(Void) touchesEnded :( NSSet) Touches withEvent :( UIEvent) Event

Before the touch ends, a system event (such as incoming call) will interrupt the touch process, and the system will automatically call the following method of view.
(Void) touchesCancelled :( NSSet) Touches withEvent :( UIEvent) Event
Tip: all objects in touches are UITouch objects.
By default, UIView processes one finger. To process Multiple fingers, you must enable Multiple Touch ).

UITouch
  • When you touch the screen with a finger, a UITouch object associated with the finger is created.
  • One finger corresponds to a UITouch object
  • Role of UITouch
    Stores finger-related information, such as the touch position, time, and phase.
  • When the finger moves, the system updates the same UITouch object so that it can keep the touch position of the finger.
  • When the finger leaves the screen, the system will destroy the corresponding UITouch object.
    Tip: iPhone development is in progress. Avoid Double-click events!
UITouch attributes
  • The window in which the touch occurs
    @ Property (nonatomic, readonly, retain) UIWindow * window;

  • View where the touch is generated
    @ Property (nonatomic, readonly, retain) UIView * view;

  • In a short period of time, you can click, double-click, or more clicks Based on tapCount.
    @ Property (nonatomic, readonly) NSUInteger tapCount;

  • The time when a touch event is generated or changed, measured in seconds.
    @ Property (nonatomic, readonly) NSTimeInterval timestamp;

  • Status of the current touch event
    @ Property (nonatomic, readonly) UITouchPhase phase;

    UITouchPhase is an enumeration type, including:
    UITouchPhaseBegan (start with touch)
    UITouchPhaseMoved (touch point Movement)
    UITouchPhaseStationary (touch points are not moved)
    UITouchPhaseEnded (touch end)
    UITouchPhaseCancelled (touch canceled)

UITouch Method

-(CGPoint) locationInView :( UIView *) view;
The returned value indicates the position of the touch on The view.
The position returned here is the coordinate system of the view (starting from the upper left corner of the view (0, 0 ))
If the view parameter passed in during the call is nil, the returned point is the position of the touch point in the UIWindow.
-(CGPoint) previuslocationinview :( UIView *) view;
This method records the position of the previous touch point.

UIEvent
  • Each time an event is generated, A UIEvent object is generated.
  • UIEvent: An event object that records the time and type of an event.
  • Common attributes
    Event Type
    @ Property (nonatomic, readonly) UIEventType;
    @ Property (nonatomic, readonly) UIEventSubtype subtype;
    Event generation time
    @ Property (nonatomic, readonly) NSTimeInterval timestamp;
  • UIEvent also provides methods to obtain the touch object (UITouch) on a view)
typedef NS_ENUM(NSInteger, UIEventType) {    UIEventTypeTouches,    UIEventTypeMotion,    UIEventTypeRemoteControl,};typedef NS_ENUM(NSInteger, UIEventSubtype) {    // available in iPhone OS 3.0    UIEventSubtypeNone                              = 0,    // for UIEventTypeMotion, available in iPhone OS 3.0    UIEventSubtypeMotionShake                       = 1,    // for UIEventTypeRemoteControl, available in iOS 4.0    UIEventSubtypeRemoteControlPlay                 = 100,    UIEventSubtypeRemoteControlPause                = 101,    UIEventSubtypeRemoteControlStop                 = 102,    UIEventSubtypeRemoteControlTogglePlayPause      = 103,    UIEventSubtypeRemoteControlNextTrack            = 104,    UIEventSubtypeRemoteControlPreviousTrack        = 105,    UIEventSubtypeRemoteControlBeginSeekingBackward = 106,    UIEventSubtypeRemoteControlEndSeekingBackward   = 107,    UIEventSubtypeRemoteControlBeginSeekingForward  = 108,    UIEventSubtypeRemoteControlEndSeekingForward    = 109,};
Touches and event parameters
  • A complete touch process goes through three states:
    Touch start:-(void) touchesBegan :( NSSet) Touches withEvent :( UIEvent) Event
    Touch movement:-(void) touchesMoved :( NSSet) Touches withEvent :( UIEvent) Event
    Touch end:-(void) touchesEnded :( NSSet) Touches withEvent :( UIEvent) Event
    Touch cancel (may experience):-(void) touchesCancelled :( NSSet) Touches withEvent :( UIEvent) Event

  • Each of the four touch event processing methods has two parameters: NSSet * touches and UIEvent * event.

    • In a complete touch process, only one event object is generated. The four touch methods are the same event parameter.
    • If two fingers touch a view at the same time, the view will only call the touchesBegan: withEvent: Method once. The touches parameter contains two UITouch objects.
    • If the two fingers touch the same view separately, the view calls the touchesBegan: withEvent: method twice, and the touches parameter for each call contains only one UITouch object.
    • Based on the number of UITouch in touches, you can determine whether it is single-point touch or multi-point touch.
Event generation and transmission
  • After a touch event occurs, the system adds the event to an event queue managed by UIApplication.

  • UIApplication extracts the first event from the event queue and distributes the event for processing. Generally, it sends the event to the application's main window (keyWindow) first)

  • The main window will find the most appropriate view in the view hierarchy to process touch events, but this is only the first step in the entire event processing process.

  • After a proper view control is found, the touches method of the View control is called for specific event processing.
    TouchesBegan...
    TouchesMoved...
    TouchedEnded...

Event transfer example

Three cases where UIView does not receive touch events
  • 1. Do not receive user interaction
    UserInteractionEnabled = NO
  • 2. Hide
    Hidden = YES
  • 3. Transparent
    Alpha = 0.0 ~ 0.01
    Tip: userInteractionEnabled of UIImageView is NO by default. Therefore, UIImageView and its sub-controls cannot receive touch events by default.
Detailed process of processing touch events
  • A touch event generated when a user clicks the screen. After a series of transmission processes, the most appropriate view control is found to process the event.

  • After finding the most appropriate view control, the touches method of the control is called for specific event processing.
    TouchesBegan...
    TouchesMoved...
    TouchedEnded...

  • The default method of these touches methods is to pass the event up the responder chain and send the event to the last responder for processing.

Responder chain

Responder chain: A chain connected by multiple responder objects
Role: You can clearly see the relationship between each responder and process multiple objects of an event.
Response object: The object that can process the event

Complete Event Transfer Process

1> first pass the event object from top to bottom (the parent control is passed to the Child control) and find the most appropriate control to process the event.
2> call touches .... Method
3> If [super touches…] is called; The event will be passed up the responder chain and passed to the last responder.
4> then, the touches of the last responder will be called .... Method
How to determine the last Responder
1> if the current view is the Controller view, the controller is the last responder.
2> if the current view is not the Controller view, the parent control is the last responder.

Event Transfer Process of the responder chain

1> If the view Controller exists, it is passed to the Controller. If the Controller does not exist, it is passed to its parent view.
2> in the top-level view of the view hierarchy, if the received event or message cannot be processed, the event or message is transmitted to the window object for processing.
3> If the window object is not processed, it will pass the event or message to the UIApplication object.
4> If UIApplication cannot process this event or message, discard it.

How to listen for touch events
  • If you want to listen to the touch event on a view, the previous method is

    • Customize a view
    • Implement the touches method of view and implement the specific processing code inside the Method
  • Using the touches method to listen to view touch events has several obvious disadvantages.

    • You must customize the view.
    • Because the touches method inside the view listens to touch events, by default, other external objects cannot listen to the view touch events.
    • It is not easy to differentiate users' specific gesture Behaviors
  • After iOS 3.2, Apple launched the Gesture Recognizer feature, which greatly simplifies developer development in terms of touch event processing.

UIGestureRecognizer
  • The UIGestureRecognizer must be used to complete gesture recognition.

  • Using UIGestureRecognizer, you can easily identify some common gestures you make on a view.

  • UIGestureRecognizer is an abstract class that defines the basic behavior of all gestures. You can use its subclass to process specific gestures.
    UITapGestureRecognizer (hitting)
    UIPinchGestureRecognizer (Kneading for scaling)
    UIPanGestureRecognizer (drag and drop)
    UISwipeGestureRecognizer (sweep)
    UIRotationGestureRecognizer (rotate)
    UILongPressGestureRecognizer (long press)

UITapGestureRecognizer

The usage of each gesture reader is similar. For example, the steps for using UITapGestureRecognizer are as follows:

  • Create a gesture identification object
    UITapGestureRecognizer * tap = [UITapGestureRecognizer alloc] init];

  • Set the attributes of the gesture reader object
    // Strike twice in a row
    Tap. numberOfTapsRequired = 2;
    // Two fingers need to be tapped together
    Tap. numberOfTouchesRequired = 2;

  • Add the gesture reader to the corresponding view.
    [Self. iconView addGestureRecognizer: tap];

  • Trigger of listening gesture
    [Tap addTarget: self action: @ selector (tapIconView :)];

Gesture Recognition Status
Typedef NS_ENUM (NSInteger, UIGestureRecognizerState) {// no touch event occurs, default status of all Gesture Recognition is unknown, // when a gesture has started but has not been changed or completed, UIGestureRecognizerStateBegan, // The gesture status changes to UIGestureRecognizerStateChanged, // The gesture completes UIGestureRecognizerStateEnded, // The gesture is canceled and restored to the Possible status failed, // The gesture fails and is restored to the Possible status failed, // recognize the gesture recognition UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded };
Gesture Recognition status changes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.