iOS event mechanism (i)

Source: Internet
Author: User
Tags uikit

iOS event mechanism (i)

DEC 7TH, 2013

The premise of application is to master
The essence of mastery is understanding

This content will be learned and analyzed around iOS events and their delivery mechanisms. In iOS, events fall into three categories:

    • Touch Events (single point, multi-touch, and various gesture actions)
    • Sensor events (gravity, acceleration sensors, etc.)
    • Remote control events (remote-controlled iOS device multimedia playback, etc.)

These three types of events together form the rich operation and experience of iOS devices, and this is the first class of events: Touch events, learning and analysis.

Gesture recognizers

Gesture recognizers is a type of gesture recognizer object that can be attached to a view you specify and set a specified gesture action for it, such as clicking, swiping, or dragging. When a touch event occurs, the gesture recognizers view is set to intercept the touch event through the recognizer, and if the touch event is a touch-aware event that was previously set for the view, gesture recognizers will send an action message to the target processing object , the target processing object will handle this touch event, first look at the following flowchart.

In iOS, view is the various UI controls we see on the screen, and when a touch event occurs, Gesture recognizers gets to the specified event and then sends an action message to the target object (target), The target object is Viewcontroller, which completes the handling of the event through the event method in Viewcontroller. Gesture recognizers can set events such as Click, Swipe, drag and drop, by action-target This design pattern, the advantage is to dynamically add a variety of event monitoring to view, instead of implementing a view subclass to complete these functions.

The above process is where we set up the action and set the target, for example, to set the listener event for UIButton in the method in development.

Common gesture Recognition classes

In the Uikit framework, the system defines a number of commonly used gesture recognizers, including click, Pinch Zoom, drag, swipe, rotate, and long press. With these gesture recognizers, we can construct a rich operating mode.

As you can see in the table above, six gesture recognizers such as UITapGestureRecognizer are already available in the Uikit framework, and if you need to implement a custom gesture recognizer, You can also do this by inheriting the Uigesturerecognizer class and overriding the methods, which we will not discuss 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 also respond to a variety of touch operations. When a touch event occurs, Gesture recognizer receives an action message prior to the view itself, and the result is a Gesture recognizer as a representation of the view-handling touch event, or an agent. When the gesture recognizer receives the specified event, it sends an action message to viewcontroller and processes it.

Continuous and discontinuous movements

Touch action is divided into continuous action (continuous) and discontinuous action (discrete), continuous action such as slide and drag, it will last for a short period of time, and the discontinuous action such as click, it will be completed in a moment, in the handling of these two kinds of events slightly different. For discontinuous actions, Gesture recognizer only sends a single action message to Viewcontoller, and for continuous actions, Gesture Recognizer will send multiple action messages to Viewcontroller until all events have ended.

There are two ways to add Gesturerecognizer to a view, one is through Interfacebuilder, the other is through code, and we look at how the code is implemented.

Myviewcontoller.m
123456789    
-(void)Viewdidload{ [SuperViewdidload]; Create and initialize a gesture object UITapGestureRecognizer*Taprecognizer=[[UITapGestureRecognizerAlloc] initwithtarget:self action: @selector  (respondtotapgesture:  //Specify action for click once  taprecognizer numberoftapsrequired = 1;  //add Gesturerecognizer[.addgesturerecognizer:taprecognizer];  //... }             /span>                

With the above code, we have implemented a click event for the current Myviewcontroller view, first constructing the UITapGestureRecognizer object, specifying the target as the current Viewcontroller itself, Action is the method of processing that is implemented at the back, which echoes the Action-target pattern mentioned earlier in this article.

In the event handling process, the two modes are in different states, first, all the touch events are at the beginning of the available state (Possible), corresponding to the Uikit inside the Uigesturerecognizerstatepossible class, If it is a discontinuous action event, the state will only transition from possible to the recognized State (recognized,uigesturerecognizerstaterecognized) or to a failed state (Failed, uigesturerecognizerstatefailed). For example, a successful click action corresponds to the possible-recognized process.

In the case of a continuous action event, if the event does not fail and the first action of the continuous action is successfully identified (recognized), the transition from the possible state to the began (Uigesturerecognizerstatebegan) state This represents the beginning of a continuous action, which is then transformed into a changed (uigesturerecognizerstatechanged) state, in which continuous operations are processed continuously until the execution of the action is changed to recognized recognized state. Eventually the action will be in the completed state (uigesturerecognizerstateended), and the processing state of the continuous action event will change from the changed state to canceled ( uigesturerecognizerstatecancelled) state, because the recognizer believes that the current action has not matched the original set of events. Each action state changes, Gesture recognizer will send a message (action message) to target, that is, Viewcontroller, which can be processed according to these action messages accordingly. For example, a successful swipe gesture involves pressing, moving, and lifting, respectively, corresponding to the possible-began-changed-recognized process.

Uitouch & Uievent

Every action event on the screen is a touch, with the Uitouch object in iOS representing each touch, multiple touches composing an event, and uievent to represent the object once.

In the above process, a two-finger scaling event action is completed, each time the change in the state of the finger corresponds to a stage in the process of event action. By began-moved-ended These phases of action (Touch) together constitute an event. There are corresponding methods in the event response object Uiresponder to handle the events of these phases separately.

    • Touchesbegan:withevent:
    • Touchesmoved:withevent:
    • Touchesended:withevent:
    • Touchescancelled:withevent:

The following parameters correspond to Uitouchphasebegan, uitouchphasemoved, uitouchphaseended, uitouchphasecancelled, respectively. Used to represent different stages of the state.

Event delivery

For example, the event delivery in iOS starts with the app (uiapplication), then passes to Window (UIWindow), and the window passes the event to Gesturerecognizer before passing it down to view, if Gesturerecognizer identifies the passed event, the event will not continue to be passed to the view, but rather to target (Viewcontroller) for processing as we said earlier.

Responder chain (Responder Chain)

Usually, in an iOS app, there are usually a lot of UI controls on a screen, that is, there are a lot of view, so when an event occurs, how to determine which view responds to the event, then we'll take a look.

Find Hit-test View

What is Hit-test view? In simple terms, the view where you trigger the event, the process of looking for hit-test view is called hit-testing. So, how does the system perform hit-testing, first of all assuming that there is a UI layout like the one that has ABCDE five view.

Assuming that a Click event occurs in view D, the system first looks from the top view a, finds that the event is in view a or its subclasses, then finds the event in C or its subclass, and then goes to C, then finds the event in D. and D has no sub-class, then hit-test view is View D.

Responder objects (Responsder object)

The responder object is an object that can respond to and handle events, and Uiresponder is the parent class for all responder objects, including UIApplication, UIView, and Uiviewcontroller, which are subclasses of Uiresponder. It also means that all view and Viewcontroller are responder objects.

First Responder (Responder)

The first responder is the first view object that receives the event, and when we draw the view in Xcode's interface builder, we can see that there is a Responder in the view structure.

The first responder here is uiapplication. In addition, we can control a view to become first Responder, by implementing the Canbecomefirstresponder method and returning Yes to make the current view the number one responder, Or you can call the Becomefirstresponder method of the view, for example, when Uitextfield calls the method, it pops up the keyboard for input, and the input box control is the first responder.

Event delivery mechanism

As stated above, if Hit-test view cannot handle the current event, then the event will be passed along the responder chain (Responder Chain), knowing that the responder (Responsder Object) who can handle the event is encountered. Let's take a look at the event passing mechanism in two different situations.

On the left, the event is received by the initial view if it cannot handle the event and she is not the top-level view, then the events are passed to its parent view. Initial view's parent view Gets the event, if it still cannot be processed, continue to pass up and loop the process. If the top view still cannot handle the event, the event is passed to their viewcontroller, and if Viewcontroller cannot handle it, it is passed to window (UIWindow). The event is passed to application (uiapplication) when the window cannot be processed, and the event is discarded if the application cannot be processed at this time.

The only difference in the flow of the diagram on the right is that if the current viewcontroller is hierarchical, then when the child Viewcontroller cannot handle the event, it will continue to pass the event up until it is passed to its root viewcontroller. The following process is the same as the previous analysis.

This is the delivery mechanism of the event responder chain, through which we can learn more about the delivery mechanism of events in iOS, it is very helpful for us to understand the principle of event operation better in actual development, as well as to add more understanding when we implement complex layout for event processing.

Summarize

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

    • Gesture recognizers is a way to control the process and method of gesture recognition, and it communicates with Viewcontroller through Action-target mode. Gesturerecognizer state transitions in the case of continuous and discontinuous gesture movements.
    • Uitouch and Uievent objects, which are objects in Uikit for event handling, and multiple Uitouch objects that form a Uievent object, overriding the appropriate methods to control and handle the actions of each stage of the event.
    • The way to find Hit-test view, event delivery machine, System responder chain

PostScript: This is the iOS event delivery mechanism of the previous article, the next section will continue to discuss multi-touch events and gesture action content!

Original address: http://ryantang.me/blog/2013/12/07/ios-event-dispatch-1/
Copyright notice: Retain Attribution-non-commercial-no deduction | Creative Commons By-nc-nd 3.0 |

Ryan's zone is being used more to say

«Using the Goagentfqios event mechanism (ii)»

iOS event mechanism (i)

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.