iOS Development gesture--uigesturerecognizer Coexistence

Source: Internet
Author: User

In the development of the IPhone or IPad, in addition to using the Touchesbegan/touchesmoved/touchesended method to control the user's finger touch, you can also use the Uigesturerecognizer derivative class to judge. The advantage of using Uigesturerecognizer is that there are ready-made gestures, and developers don't have to calculate their finger movements. Uigesturerecognizer have the following types of derivatives:

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

It is not difficult to know the gestures of these classes of representatives, such as TAP (Point), Pinch (two fingers inward or outward), Rotation (rotate), Swipe (sliding, fast moving), Pan (drag, slow motion), and Longpress (Long Press). These gestures are also very simple to use, as long as they are defined and added to the corresponding view before use.
//define a recognizer and add it to the UIView element that needs to detect the gesture- (void) viewdidload {Uiswipegesturerecognizer*recognizer; //Handleswipefrom is a way to detect gestures, to call.Recognizer =[[Uiswipegesturerecognizer alloc] initwithtarget:selfaction: @selector (Handleswipefrom)]; //different recognizer have different entity variables//For example, Swipegesture can specify the direction//and Tapgesture can specify the number of timesRecognizer.direction =Uiswipegesturerecognizerdirectionup [Self.view Addgesturerecognizer:recognizer];   [Recognizer release]; }      - (void) Handleswipefrom: (uiswipegesturerecognizer*) Recognizer {//after triggering a hand event, do something here//the bottom is the way to remove gestures.[Self.view Removegesturerecognizer:recognizer]; } 

The problem comes. Some gestures are actually interrelated, such as tap and longpress, swipe and Pan, or tap once and tap twice. When a UIView adds two associated gestures at the same time, is it a tap or a longpress on my finger? If the preset practice, as long as "meet the conditions" will jump out and call the corresponding method, for example, if you register both pan and Swipe, as long as the finger moves will trigger the Pan and then jump out, and thus will never happen Swipe, single point and two points of the same situation, It will always only trigger a single point, not a double point.

So is there a solution to this problem? The answer is yes, Uigesturerecognizer has a method called Requiregesturerecognizertofail, he can designate a certain recognizer, even if he is already satisfied with the condition, and will not be triggered immediately, would wait until the specified Recognizer is not triggered until it determines the failure. To support both single-point and double-point gestures, for example, the code is as follows:

- (void) Viewdidload {//Click the Recognizeruitapgesturerecognizer*Singlerecognizer; Singlerecognizer=[[UITapGestureRecognizer alloc] initwithtarget:selfaction: @selector (Handlesingletapfrom)]; Singletaprecognizer.numberoftapsrequired=1;//Click[Self.view Addgesturerecognizer:singlerecognizer]; //Double-click the Recognizeruitapgesturerecognizer*Double; Doublerecognizer=[[UITapGestureRecognizer alloc] initwithtarget:selfaction: @selector (Handledoubletapfrom)]; Doubletaprecognizer.numberoftapsrequired=2;//Double click[Self.view Addgesturerecognizer:doublerecognizer]; //key in this row, if you double-click to determine if the reconnaissance failure will trigger the click[Singlerecognizer Requiregesturerecognizertofail:doublerecognizer];      [Singlerecognizer release];  [Doublerecognizer release]; } 

iOS Development gesture--uigesturerecognizer Coexistence

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.