iOS Learning gestures

Source: Internet
Author: User

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:

    1. Tap gestures ( Tapgesturerecognizer)
    2. Swipe gestures ( Swipegesturerecognizer)
    3. long-Press gestures ( Longpressgesturerecognizer)
    4. Drag gestures ( Pangesturerecognizer)
    5. Pinch gesture ( Pinchgesturerecognizer)
    6. 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)

  1. 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

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.