Gesture of iOS development -- Coexistence of uigesturerecognizer

Source: Internet
Author: User

In the development of iPhone or iPad, apart from touchesbegan
/Touchesmoved/touchesended this set of methods to control the user's finger touch, you can also use the operator class of uigesturerecognizer to determine. 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 differences include tap, pinch, and rotation) swipe, pan, 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 a token to the gesture, method 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 recognizer. direction = uiswipegesturerecognizerdireup up [self. view addgesturerecognizer: recognizer]; [recognizer release];}-(void) handleswipefrom :( uiswipegesturerecognizer *) recognizer {// after triggering the manual upload event, here are some things. // The bottom is the method for removing gestures [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 a uiview is added with two associated gestures at the same time, is it a tap or longpress by the finger? According to the setting method, if the condition is set first, the corresponding method will jump out and be called. For example, if both pan and swipe are registered, as long as the finger moves, it will trigger the pan and then jump out, so the swipe will never happen. The single point is the same as the double point. It will always only trigger the single point, but not the double point.

Are you sure you want to solve this problem? The answer is yes. The uigesturerecognizer has a method called requiregesturerecognizertofail. It can specify a recognizer. Even if it has already reached the limit, it will not be triggered immediately and will wait until the specified recognizer
Triggered only after the failure is confirmed. The Code is as follows:

-(Void) viewdidload {// click the recognizer handler * singlerecognizer; singlerecognizer = [[using alloc] initwithtarget: selfaction: @ selector (handlesingletapfrom)]; then. numberoftapsrequired = 1; // click [self. view progress: singlerecognizer]; // double-click recognizer progress * double; doublerecognizer = [[initalloc] initwithtarget: selfaction: @ selector (handledoubletapfrom)]; doubletaprecognizer. numberoftapsrequired = 2; // double-click [self. view addgesturerecognizer: doublerecognizer]; // The key is in this line. If you double-click to confirm that zookeeper fails, the [singlerecognizer failed: 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.