iOS Event handling

Source: Internet
Author: User

The main thing is to record the interface touch event processing mechanism of iOS, and then use an example to illustrate the application scenario.

First, the processing mechanism

The interface response message mechanism is divided into two pieces, (1) The view is first found in the hierarchy of the view that can respond to the message. (2) The message is then processed in the found view.

The "Key" (1) process is from the parent view to the child view lookup, and (2) from the child view that is found to the parent view (not necessarily passing the message back).

1.1. The process of finding a response message view can be illustrated by borrowing a diagram of M for a J.

The processing principle is as follows:

• When the user taps the screen, a touch event is generated that will be added to an event queue managed by UIApplication

UIApplication will take the first event out of the event queue for distribution for processing, typically sending an event to the application's main window (UIWindow)

• The main window calls Hittest:withevent: Method finds the most appropriate UIView in the view (UIView) hierarchy to handle touch events

(hittest:withevent: Actually is a method of UIView, UIWindow inherit from UIView, so the main window UIWindow also belongs to the view of one)

Hittest:withevent: Method The approximate processing process is this:

First call the current view's Pointinside:withevent: method to determine whether the touch point is within the current view:

? If Pointinside:withevent: Method returns no, indicating that the touch point is not in the current view, the current view of the Hittest:withevent: return nil

? If the Pointinside:withevent: method returns Yes, indicating that the touch point is within the current view, traverse all the child views of the current view (subviews) and call the hittest:withevent of the Child View: Method repeats the previous steps, The traversal order of a child view is from top to bottom, that is, traversing forward from the end of the subviews array, until there is a hittest:withevent of the child View: The method returns a non-empty object, or all the child views are traversed:

? If the hittest:withevent of the child view is returned for the first time: The method returns a non-empty object, the current view's Hittest:withevent: method returns this object, processing ends

? If all child views are hittest:withevent: The method returns nil, the current view's Hittest:withevent: method returns the current view itself (self)

• Finally, this touch event is handed to the main window's hittest:withevent: Method returned by the view object to be processed.

After you get this uiview, call the UIView's touches series method.

1.2, the message processing process, in the found view of processing, after processing, as needed, using the response chain Nextresponder can send messages to a responder.

Uiappliactiondelegate <-uiwindow <-uiviewcontroller <-UIView <-UIView

"Key": to understand that there are three points: 1, iOS determines which interface can accept the message is from the view hierarchy of the parent view to the child view pass, that is, the root node of the tree structure to the leaf node recursive delivery. 2, HitTest and Pointinside are paired, and HitTest calls Pointinside. 3. The message processing of iOS is that when a message is processed by a person, it is no longer passed to the parent layer.

Second, the application example

"Demand" is: the interface is as follows,

Window

-viewa

-buttona

-viewb

-buttonb

Hierarchy: Viewb completely covered the BUTTONA,BUTTONB on VIEWB, now need to implement 1) Buttona and BUTTONB can respond to message 2) Viewa can also receive VIEWB messages received 3) do not let VIEWB (BUTTONB) receive the message.

(First parse, by default, click on the Buttonb area, iOS message processing.)

-viewa

-buttona

-viewb

-buttonb

When you click on the Buttonb area, the process begins: Call HitTest in turn from Viewa

The values of the pointinside are:

Viewa:no;

Viewb:yes;

Buttonb:yes;

The Subviews:no of BUTTONB;

So Buttonb's subviews hittest all return nil, so the object returned is Buttonb himself. Then proceed to the touches series method, here is the method called Buttonb binding. After processing, the message stops and the process ends. )

"Analysis":

Implemented in a variety of ways, here will be two of the requirements of the implementation, because the implementation of 2 can meet 1.

2.1, the realization of demand 1, VIEWB covered Buttona, so by default Buttona not receive the message, but in the message mechanism to find the message response is from the parent view, so we can be in the Viewa HitTest method of judgment, if touch Point is on Buttona, the Buttona is returned as a message processing object.

The code is as follows:

12345678910111213 #pragma mark - hitTest- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{    // 当touch point是在_btn上,则hitTest返回_btn    CGPoint btnPointInA = [_btn convertPoint:point fromView:self];    if([_btn pointInside:btnPointInA withEvent:event]) {        return_btn;    }        // 否则,返回默认处理    return[super hitTest:point withEvent:event];    }

This way, when the touch point is on the Buttona, then the touch message is intercepted on the Viewa, VIEWB will not be received. Then Buttona receives a touch message that triggers the OnClick method.

2.2, the realization of demand 2, above the response chain, VIEWB as long as the method of the touches series is removed, and then after their own processing, the message to the next responder (that is, the parent view is Viewa).

The code is as follows: in the VIEWB code

123456789101112131415161718192021222324252627282930 #pragma mark - touches- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{    NSLog(@"B - touchesBeagan..");        // 把事件传递下去给父View或包含他的ViewController    [self.nextResponder touchesBegan:touches withEvent:event];}- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent *)event{    NSLog(@"B - touchesCancelled..");    // 把事件传递下去给父View或包含他的ViewController    [self.nextResponder touchesBegan:touches withEvent:event];}- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent *)event{    NSLog(@"B - touchesEnded..");    // 把事件传递下去给父View或包含他的ViewController    [self.nextResponder touchesBegan:touches withEvent:event];}- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent *)event{    NSLog(@"B - touchesMoved..");    // 把事件传递下去给父View或包含他的ViewController    [self.nextResponder touchesBegan:touches withEvent:event];    }

Then, you can receive the touches message on the Viewa and write on the Viewa:

+ View Code

This enables the message to be transmitted to the parent view.

2.3, do not let VIEWB receive the message, you can set Viewb.userinteractionenable=no, in addition to this can also override Viewb Ponitinside, the principle of reference above.

Write on the VIEWB:

+ View Code

iOS Event handling

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.