Gesture of iOS development -- Coexistence of uigesturerecognizer

Source: Internet
Author: User
Gesture of iOS development -- Coexistence of uigesturerecognizer

In the development of iPhone or iPad, in addition to using the touchesbegan/touchesmoved/touchesended method to control users' finger touch, you can also use the uigesturerecognizer encoding class to make judgments. The advantage of using uigesturerecognizer is that there are ready-made gestures, so developers do not have to calculate their finger movement tracks themselves. Uigesturerecognizer has the following classes:

  • Uitapgesturerecognizer
  • Uipinchgesturerecognizer
  • Uirotationgesturerecognizer
  • Uiswipegesturerecognizer
  • Uipangesturerecognizer
  • Uilongpressgesturerecognizer

It is not difficult to understand the gestures corresponding to these classes in terms of naming. The difference is
Tap (click), pinch (move inside or outside), rotation (rotate), swipe (slide, fast move), Pan (drag, slow move) and
Longpress ). These gestures are also very easy to use, as long as you define and add them to the corresponding view before use.

// Define a recognizer and add it to the uiview component that needs to invoke the gesture
-(Void) viewdidload {
Uiswipegesturerecognizer * recognizer;
// Handleswipefrom is the method to call when the robot sends a gesture to the gesture
Recognizer = [[uiswipegesturerecognizer alloc] initwithtarget: selfaction: @ selector (handleswipefrom)];
// Different recognizer have different entity Variables
// For example, swipegesture can specify the direction
// While tapgesture can specify the number of times
Recognizer. Direction = uiswipegesturerecognizerdirectionup
[Self. View addgesturerecognizer: recognizer];
[Recognizer release];
}

-(Void) handleswipefrom :( uiswipegesturerecognizer *) recognizer {
// After the trigger of the manual failover event, do something here

// The method for removing gestures is shown below
[Self. View removegesturerecognizer: recognizer];
}

The problem arises. Some gestures are actually associated, for example, tap and longpress, swipe and pan, or tap and Tap twice. When
When two associated gestures are added to the uiview at the same time, is it a tap or
Longpress? According to the setting method, if the condition is set first, the corresponding method will jump out and be called. For example, if both the pan and
Swipe, as long as the finger moves, it will trigger the pan and then jump out, so it will never happen
The same is true for a single point and a double point. It always triggers a single point, but not a double point.

Are you sure you want to solve this problem? The answer is yes. There is a method for uigesturerecognizer called requiregesturerecognizertofail. He can specify a recognizer and it will not be triggered immediately even if he has already completed the configuration, it is triggered only when the specified recognizer fails. The Code is as follows:

-(Void) viewdidload {
// Click recognizer
Uitapgesturerecognizer * singlerecognizer;
Singlerecognizer = [[uitapgesturerecognizer alloc] initwithtarget: selfaction: @ selector (handlesingletapfrom)];
Singletaprecognizer. numberoftapsrequired = 1; // click
[Self. View addgesturerecognizer: singlerecognizer];

// Double-click recognizer
Uitapgesturerecognizer * double;
Doublerecognizer = [[uitapgesturerecognizer alloc] initwithtarget: selfaction: @ selector (handledoubletapfrom)];
Doubletaprecognizer. numberoftapsrequired = 2; // double-click
[Self. View addgesturerecognizer: doublerecognizer];

// The key lies in this line. If you double-click to confirm that the upload fails, the click will be triggered.
[Singlerecognizer requiregesturerecognizertofail: doublerecognizer];
[Singlerecognizer release];
[Doublerecognizer release];
}
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.