iOS development gesture gesture (i)

Source: Internet
Author: User

Objective  

In iOS, you can use the system's built-in gesture recognition (Gesturerecognizer), or you can create your own gestures. Gesturerecognizer the low-level conversion to high-performance execution, which is the object you bind to the view, and when a gesture occurs, the bound view object responds, and it determines whether the action corresponds to a specific gesture (swipe,pinch,pan,rotation If it recognizes this gesture, it sends a message to the view that binds it, such as

The Uikit framework provides some pre-defined Gesturerecognizer. Contains the following gestures

    • UITapGestureRecognizer tap gestures (click and double tap)
    • Uipangesturerecognizer (drag gesture)
    • Uipinchgesturerecognizer (zoom gesture)
    • Uiswipegesturerecognizer (Touch gestures)
    • Uirotationgesturerecognizer (rotation gesture)
    • Uilongpressgesturerecognizer (Long press gesture)

If you want your app to recognize a unique gesture, such as choosing a directory or Tangled motion, you can create your own custom gesturerecognizer that will be introduced in the next article

Associate a specific gesture with a view

Each specific gesture must be linked to the view object to be useful, and a view object can relate to several different specific gestures, but each specific gesture can only be associated with one view. When the user touches the view, the Gesturerecognizer receives the message, which responds to a specific touch event.

Associated with a specific view

    • Creating an Gesturerecognizer instance
    • Addgesturerecognizer
    • Ways to implement gesture handling

You can use Removegesturerecognizer: to remove gestures.

_pangesturerecognizer =[[Uipangesturerecognizer alloc] initwithtarget:self action: @selector (handlerpangesture:)]; _pangesturerecognizer.Delegate=Self ; _pangesturerecognizer.maximumnumberoftouches=2; _pangesturerecognizer.minimumnumberoftouches=2; [Self.view Addgesturerecognizer:_pangesturerecognizer];- (void) Handlerpangesture: (Uipangesturerecognizer *) recognizer{if((recognizer.state = = Uigesturerecognizerstatebegan) | |(Recognizer.state==uigesturerecognizerstatechanged)) {Cgpoint offset=[recognizer TranslationInView:self.view]; CGRect Frame=Self.rightViewController.view.frame; Frame.origin.x+=Offset.x; if(Frame.origin.x >=0&& frame.origin.x <=kscreenwidth) {Self.rightViewController.view.frame=frame;    } [recognizer Settranslation:cgpointzero InView:self.view]; }    Else if(Recognizer.state = =uigesturerecognizerstateended) {BOOL isVisible= Self.rightviewcontroller.view.frame.origin.x < Kscreenwidth/2;    [Self showrightview:isvisible]; }}

Gesture Recognition Status

  

Gesture recognizers to move from one state to another (state). For each state, you can move to the next state depending on whether they meet certain criteria. They analyze multi-touch. Whether the recognition failed. Failure to identify the gesture means that the state conversion failed. Uigesturerecognizerstatefailed. See Uigesturerecognizerstate Enumeration

typedef ns_enum (Nsinteger, uigesturerecognizerstate) {uigesturerecognizerstatepossible,//the recognizer have not yet recognized it gesture, but could be evaluating touch events. The default stateUigesturerecognizerstatebegan,//The recognizer has received touches recognized as the gesture. The action method is called at the next turn of T He run LoopUigesturerecognizerstatechanged,//The recognizer have received touches recognized as a change to the gesture. The action method would be called at the NE XT turn of the run loopUigesturerecognizerstateended,//The recognizer has received touches recognized as the end of the gesture. The action method is called at the NEX T turn of the run loop and the recognizer is reset to uigesturerecognizerstatepossibleUigesturerecognizerstatecancelled,//The recognizer have received touches resulting in the cancellation of the gesture . The action method would be called at The next turn of the run loop. The recognizer would be a reset to uigesturerecognizerstatepossibleuigesturerecognizerstatefailed,//The recognizer have received a touch sequence that can is recognized as the gesture. The action method won't be Called and the recognizer would be reset to uigesturerecognizerstatepossible//discrete gestures–gesture recognizers that recognize a discrete event but does not report changes (for example, a tap Transition through the began and Changed states and can not fail or be cancelleduigesturerecognizerstaterecognized = uigesturerecognizerstateended//The recognizer has received touches recognized as the gesture. The action method is called at the next turn of T He run Loop and the recognizer'll be reset to uigesturerecognizerstatepossible};

Add multiple gestures to a view

When multiple gestures are added to a view, the default is not to prioritize which gestures to perform, each of which occurs differently. However, you can override the default behavior (using class methods, delegate methods, and subclasses to overwrite these)

    • Specifies that one gesture recognizers should be captured before the other.

Requiregesturerecognizertofail: This method is the recipient only after the failure of the gesture recognizer as a parameter, otherwise it never happens.

[Self.panrecognizer RequireGestureRecognizerToFail:self.swipeRecognizer];
    • Allows 2 gestures to operate simultaneously

Gesturerecognizer:shouldrecognizesimultaneouslywithgesturerecognizer:

    • Prohibit the occurrence of gesture at a certain point recognizers
-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldreceivetouch: (Uitouch *) touch{     ifclass]])    {        return  NO;    }         return YES;}

Specify a one-way relationship two gesture recognizers

You want to control the interaction between two recognizers, but you need to specify a one-way relationship that you can override or Canpreventgesturerecognizer: or Canbepreventedbygesturerecognizer: a subclass method. return yes. For example, if you want a rotating stance to prevent pinching, but you do not want to clip gestures to prevent the rotation of the gesture. For example, if you want a rotation gesture to block a zoom gesture, but you don't want a zoom gesture to prevent the rotation gesture, add the following code

[Rotationgesturerecognizer Canpreventgesturerecognizer:pinchgesturerecognizer];

turn from: http://www.cnblogs.com/salam/archive/2013/04/30/iOS_gesture.html

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.