Objective
In iOS, you can use the system's built-in gesture recognition (Gesturerecognizer), or you can create your own gestures. Gesturerecognizer converts low-level to high-level execution behavior, the object you bind to the view, and when a gesture occurs, the view object that is bound to it responds and determines whether the action corresponds to a particular gesture (swipe,pinch,pan,rotation If it recognizes this gesture, it sends a message to the view that binds it, as shown below
The Uikit framework provides a number of predefined Gesturerecognizer. Contains the following gestures
- UITapGestureRecognizer Tapping Gestures (click and double-click)
- Uipangesturerecognizer (drag gesture)
- Uipinchgesturerecognizer (zoom gesture)
- Uiswipegesturerecognizer (Touch gesture)
- Uirotationgesturerecognizer (rotating gesture)
- Uilongpressgesturerecognizer (Long press gesture)
If you want your application to recognize a unique gesture, such as selecting a directory or tangled motion, you can create your own custom gesturerecognizer that will be introduced in the next chapter
Associate a specific gesture with a view
Each particular gesture must be associated with the View object, and a View object can associate several different specific gestures, but each specific gesture can only be associated with a view. When the user touches the view, the Gesturerecognizer receives a message that responds to a particular touch event.
Associating with a specific view
- Create a Gesturerecognizer instance
- Addgesturerecognizer
- How to implement gestures processing
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 = Fram
E
} [recognizer Settranslation:cgpointzero InView:self.view]; else if (recognizer.state = = uigesturerecognizerstateended) {BOOL isVisible = SELF.RIGHTVIEWCONTROLLER.VIEW.FRAME.O
Rigin.x < KSCREENWIDTH/2; [SelF showrightview:isvisible];
}
}
Gesture recognition status
gesture recognizers from one state to another (state). For each state, you can move to the next state depending on whether they meet specific criteria. They analyze multiple touch points. Whether the recognition failed. Failure to recognize gestures means that the state conversion failed. Uigesturerecognizerstatefailed. See Uigesturerecognizerstate enumeration
typedef ns_enum (Nsinteger, uigesturerecognizerstate) {uigesturerecognizerstatepossible,//The recognizer has not yet R Ecognized its gesture and but may is evaluating touch events. This is the default state Uigesturerecognizerstatebegan,//The recognizer has received touches recognized as the Gest Ure. The action method is called at the next turn of the run loop uigesturerecognizerstatechanged,//the recognizer has Received touches recognized as a change to the gesture. The action method is called at the next turn of the run loop uigesturerecognizerstateended,//the recognizer has R Eceived touches recognized as the end of the gesture. The action method is called at the next turn the run loop and the recognizer would be reset to Uigesturerecognizers Tatepossible uigesturerecognizerstatecancelled,//The recognizer has received touches resulting in the cancellation of T He gesture. The action method is called at the next turn the run loop. The recognizerwould be reset to uigesturerecognizerstatepossible uigesturerecognizerstatefailed,//The recognizer has received a TOU CH sequence that can is recognized as the gesture. The action method is called and the recognizer is reset to uigesturerecognizerstatepossible//discrete G Estures–gesture recognizers that recognize a discrete event but does not have a changes (for example, a tap) does not transi tion through the began and Changed states and can not fail/BE cancelled uigesturerecognizerstaterecognized = Uigesture recognizerstateended//The recognizer has received touches recognized as the gesture. The action method is called at the next turn the run loop and the recognizer would be reset to Uigesturerecognizers
Tatepossible};
Add multiple gestures to view
When a view adds more than one gesture, by default, it does not order which gesture to prioritize, and each occurrence is different. But you can override the default behavior (using class methods, delegate methods, and subclasses to overwrite these)
Specifies that a gesture recognizers should be captured in front of another.
Requiregesturerecognizertofail: This method occurs only after the gesture recognizer as a parameter fails, or never happens.
[Self.panrecognizer RequireGestureRecognizerToFail:self.swipeRecognizer];
Allow 2 gestures to operate simultaneously
Gesturerecognizer:shouldrecognizesimultaneouslywithgesturerecognizer:
Prohibit the occurrence of gesture recognizers at a certain point
-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldreceivetouch: (Uitouch *) Touch
{
if ([Touch.view Iskindofclass:[uicontrol class]])
{return
NO;
}
Return YES
}
Specify a one-way relationship two gesture recognizers
You want to control the interaction between the two recognizers, but you need to specify a one-way relationship that you can override or Canpreventgesturerecognizer: or Canbepreventedbygesturerecognizer: Subclass methods. return yes. For example, if you want a rotating posture to prevent pinching, but you do not want to clip gestures to prevent the rotation of the gesture. For example, you want a rotation gesture to block a zoom gesture, but you don't want a zoom gesture to prevent the rotation gesture, just add the following code
[Rotationgesturerecognizer Canpreventgesturerecognizer:pinchgesturerecognizer];
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.