IOS event mechanism (I), ios event mechanism

Source: Internet
Author: User

IOS event mechanism (I), ios event mechanism

This article describes how to learn and analyze events in iOS and their transmission mechanisms. In iOS, events are classified into three types:

  • Touch events (single point, multi-touch, and various gesture operations)
  • Sensor events (gravity, accelerometer, etc)
  • Remote Control event (Remote Control of iOS device multimedia playback, etc)

These three types of events constitute a rich set of operation methods and user experience for iOS devices. This time, we will first learn and analyze the first type of events: Touch events.

Gesture Recognizers

Gesture Recognizers is a type of Gesture Recognition object that can be attached to a specified View and set a specified Gesture operation for it, such as clicking, sliding, or dragging. When a touch event occurs, the View with Gesture Recognizers configured first intercepts the touch event through the identification device. If the touch event is a touch listening event set for the View, then Gesture Recognizers will send the action message to the target processing object, and the target processing object will process the touch event. Let's take a look at the following flowchart.

In iOS, View is the various UI controls we see on the screen. When a touch event occurs, Gesture Recognizers first obtains the specified event, then, an action message is sent to the target object, which is ViewController. In ViewController, the event is processed through the event method. Gesture Recognizers can be used to set events such as click, slide, and drag. With the Action-Target design mode, it can dynamically add various event listening for the View, instead of implementing a View subclass to complete these functions.

In the above process, we usually set actions and targets in methods during development, such as setting listener events for UIButton.

Common Gesture Recognition

In the UIKit framework, the system has defined some common gesture identifiers for us in advance, including click, double-finger scaling, drag and drop, slide, rotation, and long press. With these gesture identifiers, we can construct a variety of operation methods.

As shown in the preceding table, the UIKit framework provides six gesture identifiers, such as UITapGestureRecognizer. If you need to implement a custom gesture identifier, you can also inherit the UIGestureRecognizer class and override the method. We will not discuss it in detail here.

Each Gesture Recognizer is associated with a View, but a View can be associated with multiple Gesture Recognizer because a View may respond to multiple touch operations. When a touch event occurs, Gesture Recognizer receives an action message before the View itself. The result is that Gesture Recognizer acts as a representative of the View to process touch events, or is called a proxy. When Gesture Recognizer receives a specified event, it sends an action message to the ViewController and processes it.

Continuous and discontinuous actions

A touch action can be divided into a continuous action and a discrete action. A continuous action, such as a sliding or dragging action, lasts for a short period of time. A discontinuous action, such as a click, it will be completed in an instant, and the processing of these two types of events is slightly different. For discontinuous actions, Gesture Recognizer only sends a single action message to ViewContoller. For continuous actions, Gesture Recognizer sends multiple action messages to ViewController, until all events are completed.

There are two ways to add GestureRecognizer to a View: one is implemented through InterfaceBuilder, and the other is implemented through code. Let's see how it is implemented through code.

MyViewContoller. m

123456789101112131415
-(Void) viewDidLoad {[super viewDidLoad]; // create and initialize the gesture object Pipeline * tapRecognizer = [[externalloc] initWithTarget: self action: @ selector (respondToTapGesture :)]; // specify the operation as one-click tapRecognizer. numberOfTapsRequired = 1; // Add GestureRecognizer [self. view addGestureRecognizer: tapRecognizer]; //...}

Through the above Code, we have added a click event for the current View of MyViewController. First, we constructed the UITapGestureRecognizer object and specified the target as the current ViewController. action is the method implemented later, here we echo the Action-Target mode mentioned above.

In the event processing process, the two methods are in different States. First, all touch events are initially in the available State (Possible), corresponding to the UIGestureRecognizerStatePossible class in the UIKit, if it is a discontinuous Action event, the state will only change from Possible to Recognized (Recognized, UIGestureRecognizerStateRecognized) or Failed (Failed, UIGestureRecognizerStateFailed ). For example, a successful click action corresponds to the Possible-Recognized process.

If it is a continuous action event, if the event does not fail and the first action of the continuous action is successfully Recognized (Recognized), it is transferred from the Possible status to the Began (UIGestureRecognizerStateBegan) status, this indicates the start of the continuous action, and then changes to the Changed (UIGestureRecognizerStateChanged) State. In this state, the continuous action is continuously processed cyclically, after the action execution is completed, it changes to the Recognized State, and the action is in the completed state (UIGestureRecognizerStateEnded). In addition, the processing status of a continuous action event is Changed from Changed to Canceled (UIGestureRecognizerStateCancelled) because the identifier determines that the current action does not match the previously set event. When the status of each action changes, Gesture Recognizer sends an action message to the Target, that is, ViewController. It can process the action message accordingly. For example, a successful sliding gesture includes the process of pressing, moving, and lifting, corresponding to the Possible-Began-Changed-Recognized process.

UITouch & UIEvent

Every action Event on the screen is a Touch. In iOS, The UITouch object is used to represent each Touch. Multiple Touch forms an Event and the UIEvent is used to represent an Event object.

In the above process, a double-finger scaling event is completed, and each finger status change corresponds to a phase in the event action processing process. The Touch action in the Began-Moved-Ended phase forms an Event ). There are corresponding methods in the Event Response object UIResponder to process the events in these stages respectively.

  • TouchesBegan: withEvent:
  • TouchesMoved: withEvent:
  • TouchesEnded: withEvent:
  • TouchesCancelled: withEvent:

The following parameters correspond to the UITouchPhaseBegan, UITouchPhaseMoved, UITouchPhaseEnded, and UITouchPhaseCancelled classes. Used to indicate the status of different stages.

Event Transfer

For example, in iOS, the event is first transmitted from App (UIApplication), then to Window (UIWindow), and before being passed down to View, Window will hand over the event to GestureRecognizer, if GestureRecognizer identifies the passed event during this period, the event will not be passed to the View, but will be handed over to the Target (ViewController) for processing as we have previously said.

Responder Chain)

Generally, an iOS app usually has many UI controls on a screen, that is, there are many views. when an event occurs, how can we determine which View has responded to this event? Let's take a look.

Find hit-test view

What is hit-test view? Simply put, it is the View where the event is triggered. The process of searching for the hit-test view is called Hit-Testing. Then, how does the system execute Hit-Testing? First, assume that there is now such a UI layout, one with five views of ABCDE.

If A click event occurs in View D, the system first searches for View A at the top level and finds that the event is in View A or its subclass, then, locate B and C and find that the event is in C or its subclass. Then, search for it in C. Then, find that the event is in D and D has no subclass, the hit-test view is View D.

Responsder Object)

The responder object is an object that can respond to and process events. UIResponder is the parent class of all responder objects, including UIApplication, UIView, and UIViewController. This means that all views and ViewController are responder objects.

First Responder)

The First Responder is the First View object to receive events. When we draw a View in Xcode Interface Builder, we can see that there is First Responder in the View structure.

The First Responder here is the UIApplication. In addition, we can control a View to make it First Responder. By implementing the canBecomeFirstResponder method and returning YES, we can make the current View the First Responder, or call the becomeFirstResponder method of the View, for example, when UITextField calls this method, a keyboard is displayed for input. The input box control is the first responder.

Event Transfer Mechanism

As mentioned above, if the hit-test view cannot process the current event, the event will be transmitted along the Responder Chain, responsder Object ). Let's take a look at the event transfer mechanism in two different cases.

On the left side, if the initial view of the received event cannot process the event and is not the top-level View, the event will be passed to its parent View. If the parent view of the initial View cannot be processed after it obtains the event, it will continue to be passed up and loop through this process. If the top-level View still cannot process this event, it will pass the event to their ViewController. If the ViewController cannot process the event, it will be passed to Window (UIWindow ), if the Window cannot be processed, the event will be passed to the Application (UIApplication). If the Application cannot be processed, the event will be discarded.

The only difference in the process of the right-side graph is that if the current ViewController is hierarchical, when the sub-ViewController cannot process the event, it will continue to pass the event upwards, the subsequent process is the same as the previous analysis until it is passed to its Root ViewController.

This is the transfer mechanism of the event responder chain. Through this content, we can gain a deeper understanding of the transfer mechanism of events in iOS, it is of great help for us to better understand the principles of event operations in actual development, and also adds an additional understanding for us to implement complex layout for event processing.

Summary

Through the previous content analysis, we have learned and learned the following:

  • Gesture Recognizers is used to control the process and method of Gesture recognition, and communicates with ViewController through Action-Target mode. GestureRecognizer status changes in the case of continuous and discontinuous gestures.
  • UITouch and UIEvent objects are all the objects for event processing in UIKit. Multiple UITouch objects constitute a UIEvent object, and the corresponding method can be rewritten to control and process operations at various stages of the event.
  • The method of searching for hit-test view, the event transmitter, and the response chain

Postscript: This is the last article on iOS event transfer mechanism. The next article will continue to discuss the content of multi-touch events and gesture operations!

Related Article

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.