iOS Development UI Chapter-Event delivery

Source: Internet
Author: User

iOS gestures are an integral part of iOS development, but for iOS gestures and events It's not known, so let's talk about the iOS event mechanism today.

First, the event delivery of iOS is passed by the "event chain", and the event stops passing when an object on one of the chain loops is processed. So the chain of events is God horse thing?

An event chain is an object that consists of a set of Uiresponder (the base class capable of responding to time), of course, the event header and the end of the event, which are passed from the beginning to the next, asking whether to handle the current event. If you deal with it, stop passing it.

How an app's "chain of events" is built and what is needed to build it.

We first say the structure of the app, the General app is a appdelegate and a application, then add window, add Rootview, in addition to other view such as

Figure 1

As you can see, the order in which the app is generated, first appdegate and uiapplication, then window, down is a variety of view, and the event chain is linked on the basis of this structure, forming a tree structure.

You can see the red labels and arrows, this is the order of the chain of events. So how does the chain of events occur, with the following two scenarios.

1.addsubview, add a normal view B to a and then B's Nextresponder (the next responder) is assigned a.

2. If a veiw is not a normal view, it is the root of a viewcontroller veiw, the nextresponder of a,a in the example is assigned Viewcontrollera after a has been initialized. Then when a is addsubview to Rootview, the nextresponder of Controller A is copied to Rootview.

The chain of events is not built in one place at a time, but is triggered when the addsubview is generated or when the view is initialized in Viewcontroller. The only trigger is two objects, Superview and Subview, or Viewcontroller of Superview and Subview.

Since the chain of events is a tree structure, events cannot be passed between the brothers of the view.

Add, UIApplication's Nextresponder is AppDelete

After the event chain, how the down event is passed.

First, when the user touches, Uikit first creates an event object, adds it to the event queue, then uiapplication it out, then determines the chain of events, then asks for processing in turn.

How to determine the chain of events.

There is a hittesting process. As follows

First find the window,

1. Call Hittest:withevent:,hittest:withevent: First call Pointinside:withevent: To determine whether the event occurred in its own area

2. If yes is returned, the 1 procedure is called from each subview of window. And then layer by layer call down to find the top view.

Of course Hittest:withevent: In addition to pointinside:withevent: At the same time, the view's userinteractionenabled and view's hidden properties and alpha values are also referenced, when view is hidden, Either userinteractionenabled is no, or alpha is less than 0.01, hittest:withevent: Returns No. For example, view A and view B, such as

Figure 2

The Superview of A and B first hittest:withevent: On B, found that B is hidden, or userinteractionenabled is no, or alpha is less than 0.01, then it abandons B and instead checks a.

The above is to determine the chain of events, and then pass it is very simple. But with the gesture, it's a little bit more complicated.

First of all, each of our gestures is actually a series of uievent.

1, each time there is one, he will start from the front of the chain of events to pass, first of all the event to pass to the top of the chain of view gestures. Then see if there is no recognition, if not continue to the next responder's gestures, if recognized immediately, then stop passing. If the whole chain is passed, and no recognition has been found, how to do it. Don't assume that the event is lost, the event will go back to the front of the chain, and then start looking for the first view that implements Touch (Touchbegin,touchend, and so on) to identify, and then stop this pass.

2. Remove one from the time and proceed with the process. End to no event location.

These are the entire process of passing and recognizing events.

iOS Development UI Chapter-Event delivery

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.