iOS gesture processing

Source: Internet
Author: User

There are several iOS gestures:

    • UITapGestureRecognizer
    • Uipinchgesturerecognizer
    • Uirotationgesturerecognizer
    • Uiswipegesturerecognizer
    • Uipangesturerecognizer
    • Uilongpressgesturerecognizer

The above gestures correspond to the following actions:

  • Tap (TAP)
  • Pinch (two fingers inward or outward toggle, usually used in scaling) matrix transformation
  • Rotation (rotation) matrix transformation
  • Swipe (sliding, fast moving)
  • Pan (drag, slow motion) matrix Transformation
  • Longpress (Long press )

Note: The following example encapsulates gestures into a view

UITapGestureRecognizer-tap gestures

GestureView.h + gestureview.m

GestureView.hgestureview.m

-(void) Addgesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer

Attaching a gesture recognizer to a view defines the scope of the represented gesture, causing it to receive touches hit-t ested to this view and all of its subviews. The view establishes a strong reference to the gesture recognizer.

Attaching a gesture recognizer to a view actually defines the area where a gesture is received, passing the received touch event to the view and all the subviews of the view. This view will strongly refer to this gesture recognizer.

Two points can be summed up:

1. Gestures are passed to all the subviews in this view

2. View will strongly reference gesture recognizer

Use the following:

Click gestures have two parameters to set:

Numberoftapsrequired Click Trigger Event (default is 1)

Numberoftouchesrequired requires several fingers to click (Default is 1)

Uipinchgesturerecognizer -Zoom

GestureView.h + gestureview.m

GestureView.hgestureview.m

The zoom gesture uses a matrix transformation.

Uirotationgesturerecognizer-Rotate

GestureView.h + gestureview.m

GestureView.hgestureview.m

Uiswipegesturerecognizer-Sliding

GestureView.h + gestureview.m

GestureView.hgestureview.m

Uipangesturerecognizer-Panning

GestureView.h + gestureview.m

GestureView.hgestureview.m

Uilongpressgesturerecognizer-long press gesture

GestureView.h + gestureview.m

GestureView.hgestureview.m

Question: How to handle a view with two gestures added, one for the click Gesture, and one for the double-tap gesture?

You can use this method to Requiregesturerecognizertofail:

gestureview.m

In fact, this approach will have a sense of delay-_-!!!!

Question: How do I combine long-press gestures with drag gestures?

We need to implement it with a proxy to implement the following methods:

-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: (Uigesturerecognizer *) Othergesturerecognizer

Asks the delegate if, gesture recognizers should is allowed to recognize gestures simultaneously.

Ask the agent if it allows two gestures to be triggered at the same time.

gestureview.m

It is important to identify the full details of the gesture triggering event based on the gesture status.

Question: How do I get a partial area of a view to respond to a drag event?

For example, we only need the area of the following red line to respond to the drag event:

gestureview.m

To achieve that effect, the following method is the core method, using the state of the gesture:

Gets the point to touch in the current view coordinates of the current gesture
Cgpoint point = [Sender locationinview:self];

Question: How do I get the coordinates of the click in the Viewcontroller and let a view follow the touch point move?

You can use these most primitive methods of handling touch events to achieve results.

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event;
-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event;
-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event;

ROOTVIEWCONTROLLER.M

You can also use a drag gesture directly, but not perfect.

ROOTVIEWCONTROLLER.M

The contrasting relationship between them:

The central place in gesture processing:

1. Uigesturerecognizerstate is very important, when triggering an event, you can determine the sequence of events directly based on the status value.

2. When dealing with multiple gesture collisions, you can use the dependency requiregesturerecognizertofail: to handle it, but the effect is not good

3. When dealing with multiple gestures concurrent responses, you need to implement the agent and execute the method, please refer to the above example

4. To handle only a local gesture event on a view, you need to use the locationinview of the gesture: method and work with the Uigesturerecognizerstate state value

Appendix:

-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldreceivetouch: (Uitouch *) touch

This is the proxy method of the gesture, and the response to the gesture can be closed without removing the gesture, which involves the response chain.

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.