Uigesturerecognizer
In order to complete gesture recognition, it is necessary to use the gesture recognizer--uigesturerecognizer, using uigesturerecognizer, Easily identify Some common gestures that users make on a view Uigesturerecognizer is an abstract class that defines the basic behavior of all gestures. Use its subclasses to handle specific gestures, to implement <uigesturerecognizerdelegate>
Gesture Status:
typedef ns_enum (Nsinteger, uigesturerecognizerstate) {uigesturerecognizerstatepossible,//No gesture action has been identified (but may have triggered a touch event), default stateUigesturerecognizerstatebegan,//The gesture has already started and is now recognized, but the process may change and the gesture has not been completeduigesturerecognizerstatechanged,//gesture State changesuigesturerecognizerstateended,//gesture recognition is complete (the finger is now released)uigesturerecognizerstatecancelled,//The gesture is canceled and reverts to the default stateuigesturerecognizerstatefailed,//gesture recognition failed, reverting to default stateuigesturerecognizerstaterecognized= uigesturerecognizerstateended//gesture recognition complete, with uigesturerecognizerstateended};
Six commonly used gestures:
- Tap gestures ( Tapgesturerecognizer)
- Swipe gestures ( Swipegesturerecognizer)
- long-Press gestures ( Longpressgesturerecognizer)
- Drag gestures ( Pangesturerecognizer)
- Pinch gesture ( Pinchgesturerecognizer)
- Rotation gesture ( Rotationgesturerecognizer)
1. tap gestures (tapgesturerecognizer)
1) Initializes the tap Gesture Recognizer Object UITapGestureRecognizer*tap =[[UITapGestureRecognizer alloc] init];2sets the specific properties of the gesture Recognizer Object//2 consecutive Strokestap.numberoftapsrequired=2;//It takes 2 fingers to tap together .tap.numberoftouchesrequired=2;//add a gesture recognizer to the corresponding view[Self.iconview Addgesturerecognizer:tap];3) The trigger of the listening gesture [tap addtarget:self action: @selector (Tapiconview:)];4) agent tap.Delegate=Self ;//tap Gesture Trigger method-(void) Tapiconview: (UITapGestureRecognizer *) sender{NSLog (@"I hit the screen with two fingers! ");}#pragmaMark-Proxy method/** * This method is called when the view is clicked*/-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldreceivetouch: (Uitouch *) touch{Cgpoint Pos=[Touch LocationInView:touch.view]; if(Pos.x <= Self.iconView.frame.size.width *0.5) { returnYES; } returnNO;}
2. Swipe gesture (swipegesturerecognizer)
Initialize the swipe gesture Recognizer Object Uiswipegesturerecognizer*swipegesture =[[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector (swipegesture:)];//set the right direction for swipingswipegesture.direction= Uiswipegesturerecognizerdirectionright;//Right[Self.view addgesturerecognizer:swipegesture]; Uiswipegesturerecognizer*swipegestureleft =[[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector (swipegesture:)];//set the direction of the swipeswipegestureleft.direction= Uiswipegesturerecognizerdirectionleft;//left[Self.view Addgesturerecognizer:swipegestureleft];//Swipe gesture Trigger method-(void) Swipegesture: (ID) Sender{uiswipegesturerecognizer*swipe =Sender;if(Swipe.direction = =uiswipegesturerecognizerdirectionleft) {//Swipe left}if(Swipe.direction = =uiswipegesturerecognizerdirectionright) {//Swipe right}}
3. long-Press gestures ( Longpressgesturerecognizer )
Initialize the swipe gesture Recognizer Object Uilongpressgesturerecognizer*longpressgesture =[[Uilongpressgesturerecognizer alloc] initwithtarget:self action: @selector (longpressgesture:)];//set long by Timelongpressgesture.minimumpressduration=0.5; [Self.view addgesturerecognizer:longpressgesture];//long-Press gesture trigger method-(void) Longpressgesture: (ID) sender{Uilongpressgesturerecognizer*longpress =Sender;if(Longpress.state = =Uigesturerecognizerstatebegan) { //Your code}} Description: The common status of the long-press gesture starts as follows: Uigesturerecognizerstatebegan change: uigesturerecognizerstatechanged end: uigesturerecognizerstateended Cancel : uigesturerecognizerstatecancelled failure: uigesturerecognizerstatefailed
4. Drag gestures (pangesturerecognizer)
*pangesture = [[Uipangesturerecognizer alloc] initwithtarget:self action: @selector (pangesture:)];[ Self.view Addgesturerecognizer:pangesture]; // drag gesture Trigger method -(void) Pangesture: (ID) sender{ *pangesture = sender; = [Pangesture translationInView:self.view]; // Your code }
5. pinch gesture (pinchgesturerecognizer)
*pangesture = [[Uipangesturerecognizer alloc] initwithtarget:self action: @selector (pangesture:)];[ Self.view Addgesturerecognizer:pangesture]; // drag gesture Trigger method -(void) Pangesture: (ID) sender{ *pangesture = sender; = [Pangesture translationInView:self.view]; // Your code }
6. rotation gesture (rotationgesturerecognizer)
Initialize the swipe gesture Recognizer Object Uirotationgesturerecognizer*rotationgesture =[[Uirotationgesturerecognizer alloc] initwithtarget:self action: @selector (rotationgesture:)];[ Self.view Addgesturerecognizer:rotationgesture];//rotation gesture Triggering method-(void) Rotationgesture: (ID) Sender{uirotationgesturerecognizer*gesture =Sender;if(gesture.state==uigesturerecognizerstatechanged) {_imageview.transform=cgaffinetransformmakerotation (gesture.rotation);}if(gesture.state==uigesturerecognizerstateended) {[UIView animatewithduration:1animations:^{_imageview.transform=cgaffinetransformidentity;//Cancel the deformation}];}}
iOS Learning gestures