Event Handling Guide for iOS Reading Notes (1) Gesture Recognition

Source: Internet
Author: User

Event Handling Guide for iOS Reading Notes (1) Gesture Recognition

 

Gesture Recognizers Gesture Recognition

 


 

Overview:

1. The application can get events from the user's touch VIew.

2. Applications can get events from users' mobile devices.

3. Applications can obtain remote control events from users' multimedia operations (such as volume control from headphones)

Gesture Recognizers Gesture Recognition

Gesture Recognition is a process from a low-level event to a high-level event through code. When you touch a view, Gesture Recognition recognizes the operation, such as swipe, pinch, or rotation. if this operation is identified, the gesture will send a message to the target. target is usually a controller.

It is more convenient to use the built-in gesture reader. You can also customize the reader, which will be described below:

 

Gesture identifiers are bound to a view. A view can also be composed of multiple gesture identifiers.

Gestures can be divided into independence and continuity. For example, if a tap is an independent event, pinching is a continuous event. The gesture recognition device sends a message to the target after recognizing the gesture.

 

Three steps are required to create a system gesture reader:

1. Create a reader instance, including specifying target, action, and special attributes, such as numberOfTapsRequired.

2. Bind the reader to the view

3. Implement the action method and handle the event

 

How Gesture Recognition works

The recognition sequence is determined by a series of States. status description is defined in UIGestureRecognizerState enumeration. from one status to another, it may vary depending on the current situation. describes the status transition of Independent Events and continuous events:

Once the status reaches the Recognied identifier, a message is sent to the target, and the changed message is sent to the target multiple times.

 

Gesture Interaction

A view can contain multiple gesture identifiers. You can view these identifiers in the view attribute gestureRecognizers. You can also use addGestureRecognizer: and removeGestureRecognizer to add and remove gesture identifiers. generally, multiple gestures in a view are unordered and the order of each recognition may be different. You can implement these actions through UIGestureRecognizer methods, proxy methods, or inheritance:

1. Specify that one reader is recognized before another.

2. allow two identifiers to work simultaneously

3. prevent a reader from working

You can also define the trigger sequence of two gesture identifiers, which can be processed using the UIGestureRecognizer class method.

 

-(Void) requireGestureRecognizerToFail :( UIGestureRecognizer *) otherGestureRecognizer; // This gesture is not processed until another gesture fails.

 

Prevents gesture recognition from processing UIGestureRecognizerDelegate using the gesture proxy method

 

Both proxies can block gestures.

 

-(BOOL) gestureRecognizerShouldBegin :( UIGestureRecognizer *) gestureRecognizer;

-(BOOL) gestureRecognizer :( UIGestureRecognizer *) gestureRecognizer shouldReceiveTouch :( UITouch *) touch;

When the touch starts and you can immediately determine whether a gesture needs to start, you can use gestureRecognizer: shouldReceiveTouch: proxy method. This method is called every time a new touch occurs.

If you need to wait long enough to determine whether a gesture starts, you can use the gestureRecognizerShouldBegin: proxy method, which is called when the reader is about to switch from the state possible to the next state.

 

Allow two gestures to be recognized at the same time

By default, the two identifiers cannot be recognized at the same time. However, if you want to use both amplification and rotation, you need to configure them. You can use the proxy method.

GestureRecognizer: shouldRecognizeSimultaneouslyWithGestureRecognizer: to set.

 

Interaction with other interface control controls

In iOS6 and later versions, the gesture of the default system control cannot be overwritten. for example, the default UIButton click event is a single tap. If you have a custom single tap gesture bound to UIButton, the button action method is called after you click the button, instead of triggering a single tap gesture. there are many other similar features, such as adding swip gestures to UISlider and adding pan gestures to UISwitch. of course, you can also inherit system components to change the event response.

 

An event contains a sequence of all current multi-point touch. A gesture can have one or more touch represented by a UITouch object. for example, a pinch-and-close gesture contains two touches-two fingers slide in the opposite direction on the screen. A multi-point touch is a sequence of time from the touch of the finger to the screen, to the time when the last finger leaves the screen. when the finger moves on the screen, the system will send events represented by UITouch objects. The multi-touch time is represented by UITouch type UIEventTypeTouches. each UITouch object can only trace the activity of one single finger. During finger sliding, UIKit updates UITouch attributes, including the touch stage and position on the touch View, its previous position and timestamp.

 

A touch stage indicates when the touch starts, when it moves or stops, and when it ends.

 

When a touch changes, the system will call the following method to notify the application.

● TouchesBegan: withEvent: method when one or more fingers touch down on the screen.
● TouchesMoved: withEvent: method when one or more fingers move.
● TouchesEnded: withEvent: method when one or more fingers lift up from the screen.

● TouchesCancelled: withEvent: method when the touch sequence is canceled by a system event, such as an incoming phone call.

Each method is associated with a touch phase

 

Adjust the transmission and response of UITouch between uiviews

There may be many times when you need a View to receive Touch events before the gesture reader identifies them, but before you change the Touch Transmission sequence, you 'd better understand the default operation behavior of the system.

By default, the touch event is transmitted from UIApplication to the UIWindow object. UIWindow sends the event to any gesture identification device bound to the touched View.

UIWindow will delay the touch object to the View, so that during this delay, the reader can recognize the gesture. If the gesture is identified, UIWindow will not pass any touch object to this view. and cancel the touch sequence sent to this view.

 

Assume that there is a discontinuous gesture that requires two fingers to touch, which will be converted into two separated touch objects. When the touch starts, the touch object is uploaded from the application to the view in the window.

The message transmission sequence of this touch is as follows:

1. in the began stage, window sends two touch objects. The identifiers are not recognized by touchesBegan: withEvent: method, so the status is Possible. window sends these messages to the view bound to the gesture trigger.

2. In the move phase, window sends two touch objects-they are identified by touchesMoved: withEvent: method. The identifier still does not recognize them, so the status is Possible. The message will be transmitted to the view.

3. window sends a touch object in the ended stage-it is identified by touchesEnded: withEvent: method. The identifier does not give up because of insufficient information, but the message will not be transmitted to the view.

4. window sends out another touch object in the ended stage. At this time, the trigger is triggered and the gesture is found, so the status is changed to recognized. view calls touchesCancelled: withEvent: Method to cancel the trigger method of the previous touch object. The final phase of the two touch objects is canceled.

 

Affects the transfer of touch objects to view

You can change the attribute values of several UIGestureRecognizer classes to change the default touch transfer path. If you change the default values of the following attributes, you will get different behaviors:

 

Attribute @ property (nonatomic) BOOL delaysTouchesBegan; default value: NO. Generally, window sends the touch object to the view in the an and move phases. If delaysTouchesBegan is set to YES, it will block the attribute @ property (nonatomic) BOOL delaysTouchesEnded; default value: YES -- when set to YES, it ensures that when the gesture is canceled, the view will not respond to the event, during the recognition process, window does not pass the event object to the bound view. if the identifier identifies a gesture, it will cancel the touch event. If it does not, it will trigger the-touchedEnded: withEvent method. if NO is set, touch time and gesture recognition are allowed to be triggered simultaneously. for example, a view is bound with a tap gesture and needs to be clicked twice. when we double-click it, if it is set to YES, the trigger method order is 1. touchesBegin: withEvent: 2. touchedBegin: withEvent 3. touchesCancelled: withEvent 4. touchesCancelled: withEvent: If NO is set, the trigger sequence is: 1. touchesBegin: withEvent: 2. touchesEnded: withEvent: 3. touchesBegin: withEvent: 4. touchedCancelled: withEvent. create a custom gesture identification device
First create a UIGestureRecognizer subclass, and then introduce the header file # import To implement the following method, you must first call the super method of the parent class.
- (void)reset;- (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;
In addition, we also noticed that the state attribute in the UIGestureRecognizerSubclass header file has changed to readwrite and read-only, rather than the original read-only

 

Reset the status state of the gesture Reader

When the recognition status of our identifier changes (UIGestureRecognizerState) and prevents further recognition, the identifier calls the-reset method to reset the internal status.

 

 

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.