Event handling guide for iOS

Source: Internet
Author: User

Reference

[1] http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/Introduction/Introduction.html#//apple_ref/doc/uid/TP40009541-CH1-SW1

1. Event Type

It mainly involves three types of events: Touch, action, and remote control.

Typedef Enum {
Uieventtypetouches,
Uieventtypemotion,
Uieventtyperemotecontrol,
} Uieventtype;

2. accelerator and Gyroscope

Uiaccelerometer and uiacceleration may be deprecated in the future. Therefore, you should use the interfaces in Core Motion framework.

Notes:The uiaccelerometer anduiacceleration classes will be deprecated in a future release, so if your application handles accelerometer events, it shoshould transition to the core
Motion API.

In IOS 3.0 and later, if you are trying to detect specific types of motion as gestures-specifically shaking motions-You shocould consider handling Motion Events (uieventtypemotion)
Instead of using the accelerometer interfaces. if you want to receive and handle high-rate, continuous motion data, you shoshould instead use the Core Motion accelerometer API. motion Events are described in "shaking-Motion
Events ."

3. Event Response chain

Simply put, view-> viewcontroller-> supperview is in such a loop. The procedure is as follows:

  1. The hit-test view or first responder passes the event or message to itsview controller if it has one; if the view doesn't have a view controller, it passes the event or message to its superview.
  2. If a view or its view controller cannot handle the event or message, it passes it to thesuperview of the view.
  3. Each subsequent superview in the hierarchy follows the pattern described in the first two steps if it cannot handle the event or message.
  4. The topmost view in the view hierarchy, if it doesn't handle the event or message, passes it to thewindow object for handling.
  5. The uiwindow object, if it doesn' t handle the event or message, passes it to thesingleton
    Application object.

If the application object cannot handle the event or message, it discards it.

4. hittest: Find the view in the vertex

When a user clicks on the screen, the system looks for the view where the user is currently clicked. If the view is hidden and transparent, the userinteractionenabled is no and is not included in the search target.

The view of the system query response is from top to bottom, and the event response chain is from bottom to top.

The details are as follows:

  • Touch events. The window object uses hit-testing and the responder chain to find the view to receive the touch event. In hit-testing, a window callshittest: withevent:
    On the top-most view of the view hierarchy; this method proceeds by recursively callingpointinside: withevent:
    On each view in the view hierarchy that returnsyes, proceeding down the hierarchy until it finds the subview within whose bounds the touch took place. That view becomes the hit-test view.

Easy to understand. After copying a piece of code, the system implementation should be similar.

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{    return CGRectContainsPoint(self.bounds, point);}- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{    if (self.hidden || !self.userInteractionEnabled || self.alpha < 0.01 || ![self pointInside:point withEvent:event]) {        return nil;    } else {        for (UIView *subview in [self.subviews reverseObjectEnumerator]) {            UIView *hitView = [subview hitTest:[subview convertPoint:point fromView:self] withEvent:event];            if (hitView) {                return hitView;            }        }        return self;    }}

5. Gesture Recognition

The system supports 6 basic gestures.

Tapping
(Any number of taps) uitapgesturerecognizer
Pinching in and out (for zooming a view) uipinchgesturerecognizer
Panning or dragginguipangesturerecognizer
Swiping (in any direction) uiswipegesturerecognizer
Rotating (fingers moving in opposite directions ctions) uirotationgesturerecognizer
Long press (also known as "touch and hold") uilongpressgesturerecognizer

The method used is to create the corresponding instance, and add addgesturerecognizer to the corresponding view. When it is identified, the return function of the response is called.

To implement your own gesture recognition, inherit from uigesturerecognizer, and rewrite the following four methods to implement the gesture recognition algorithm. For more information, see the document.

-(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;

Nothing special.

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.