Six types of gesture recognition in iOS development

Source: Internet
Author: User

There are six types of gesture recognition in iOS development:

Tap gestures (Tapgesturerecognizer),

Swipe gesture (Swipegesturerecognizer),

Long-press gesture (Longpressgesturerecognizer),

Drag gestures (Pangesturerecognizer),

Pinch gesture (Pinchgesturerecognizer),

Rotation gesture (Rotationgesturerecognizer),

1, tap gesture (Tapgesturerecognizer)
UITapGestureRecognizer *tapgesture = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (tapgesture :)];tapgesture.numberoftapsrequired = 1; Number of clicks tapgesture.numberoftouchesrequired = 1; Click Hand Index [Self.view addgesturerecognizer:tapgesture];//tap Gesture Trigger Method-(void) Tapgesture: (UITapGestureRecognizer *) sender{    //your Code}
2, long press gesture (Longpressgesturerecognizer)
Uilongpressgesturerecognizer *longpressgesture = [[Uilongpressgesturerecognizer alloc] initwithtarget:self action:@ Selector (longpressgesture:)];//set long by time longpressgesture.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 general status of the long-press gesture starts as follows: Uigesturerecognizerstatebegan change: uigesturerecognizerstatechanged end: uigesturerecognizerstateended Cancel: UIGest urerecognizerstatecancelled failure: uigesturerecognizerstatefailed
3, swipe gesture (Swipegesturerecognizer)
Uiswipegesturerecognizer *swipegesture = [[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector ( Swipegesture:)];//set the direction of the swipe swipegesture.direction = uiswipegesturerecognizerdirectionright; Right [Self.view addgesturerecognizer:swipegesture]; Uiswipegesturerecognizer *swipegestureleft = [[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector ( Swipegesture:)];//set the direction of the swipe swipegestureleft.direction = Uiswipegesturerecognizerdirectionleft; Left [Self.view addgesturerecognizer:swipegestureleft];//swipe gesture trigger Method-(void) Swipegesture: (ID) sender{ Uiswipegesturerecognizer *swipe = sender;if (swipe.direction = = Uiswipegesturerecognizerdirectionleft) {//left swipe}if ( Swipe.direction = = uiswipegesturerecognizerdirectionright) {//Right Swipe}}
4, pinch gesture (pinchgesturerecognizer)
Uipinchgesturerecognizer *pinchgesture = [[Uipinchgesturerecognizer alloc] initwithtarget:self action: @selector ( Pinchgesture:)]; [Self.view addgesturerecognizer:pinchgesture];////Pinch gesture trigger Method-(void) Pinchgesture: (ID) sender{ Uipinchgesturerecognizer *gesture = sender;//gesture change if (gesture.state = = uigesturerecognizerstatechanged) {// Pinch gesture scale attribute record scaling _imageview.transform = Cgaffinetransformmakescale (Gesture.scale, Gesture.scale);} After end recovery if (gesture.state==uigesturerecognizerstateended) {[UIView animatewithduration:0.5 animations:^{_ Imageview.transform = cgaffinetransformidentity;//Cancel all deformation}];}}
5, drag gesture (Pangesturerecognizer)
Uipangesturerecognizer *pangesture = [[Uipangesturerecognizer alloc] initwithtarget:self action: @selector (pangesture :)]; [Self.view addgesturerecognizer:pangesture];//Drag Gesture Trigger Method-(void) Pangesture: (ID) sender{    uipangesturerecognizer * Pangesture = sender;    Cgpoint movepoint = [Pangesture TranslationInView:self.view];    Your code}
6, rotation gesture (Rotationgesturerecognizer)
Uirotationgesturerecognizer *rotationgesture = [[Uirotationgesturerecognizer alloc] initwithtarget:self action:@ Selector (rotationgesture:)]; [Self.view addgesturerecognizer:rotationgesture];//Rotation gesture Trigger Method-(void) Rotationgesture: (ID) sender{ Uirotationgesturerecognizer *gesture = sender;if (gesture.state==uigesturerecognizerstatechanged) {_ Imageview.transform=cgaffinetransformmakerotation (gesture.rotation);} if (gesture.state==uigesturerecognizerstateended) {[UIView animatewithduration:1 animations:^{_imageview.transform =cgaffinetransformidentity;//cancel the deformation}];}}

Six types of gesture recognition in iOS development

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.