iOS gesture recognizer

Source: Internet
Author: User

Tags: for long press add EBE NSF recognition cgpoint article Anim

Uigesturerecognizer

The Uigesturerecognizer class is used to detect and identify gestures used by users when they use the device. It is an abstract class that defines the basic behavior of all gestures. The following is a Uigesturerecognizer subclass that handles specific user gesture behavior:

UITapGestureRecognizer//1. Click
Uilongpressgesturerecognizer//3. Long Press
Uiswipegesturerecognizer//4. Swipe
Uipangesturerecognizer//5. Mobile
Uirotationgesturerecognizer//6. Rotate
Uipinchgesturerecognizer//7. Kneading

To create a gesture:
    1. Click uitapgesturerecognizer *tap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (Tapactio    N:)];        [Imgview Addgesturerecognizer:tap]; 2. Double-click uitapgesturerecognizer *doubletap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (DOUBL    Etapaction:)];    doubletap.numberoftapsrequired = 2;        [Imgview Addgesturerecognizer:doubletap];        Double-clicking failed to click [Tap Requiregesturerecognizertofail:doubletap]; 3. Long press Uilongpressgesturerecognizer *longpress = [[Uilongpressgesturerecognizer alloc] initwithtarget:self action: @se    Lector (longpressaction:)];    Set the shortest time longpress.minimumpressduration = 1;            [Imgview addgesturerecognizer:longpress]; 4. Swipe Uiswipegesturerecognizer *swipe = [[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector (swipe    Action:)];    Set the swipe direction [swipe setdirection:uiswipegesturerecognizerdirectionright];        [Imgview Addgesturerecognizer:swipe]; 5. Mobile UipangeSturerecognizer *pan = [[Uipangesturerecognizer alloc] initwithtarget:self action: @selector (panaction:)];        [Imgview Addgesturerecognizer:pan];        Swipe failed to move [Pan requiregesturerecognizertofail:swipe]; 6. Rotate Uirotationgesturerecognizer *rotation = [[Uirotationgesturerecognizer alloc] initwithtarget:self action: @selec    Tor (rotationaction:)];        [Imgview addgesturerecognizer:rotation]; 7. Kneading Uipinchgesturerecognizer *pinch = [[Uipinchgesturerecognizer alloc] initwithtarget:self action: @selector (pinch    Action:)]; [Imgview Addgesturerecognizer:pinch];
Gesture Trigger Event:

Gestureaction:

-(void) Longpressaction: (Uilongpressgesturerecognizer *) longpress{if (longpress.state = =    Uigesturerecognizerstatebegan) {NSLog (@ "long press start");    }else if (longpress.state = = uigesturerecognizerstateended) {NSLog (@ "long press End");    }}-(void) Panaction: (Uipangesturerecognizer *) Pan {//the coordinates where the finger is cgpoint point = [Pan LocationInView:self.view]; _view.center = point;} -(void) Rotationaction: (Uirotationgesturerecognizer *) rotation{if (rotation.state = =                uigesturerecognizerstatechanged) {//take to radians cgfloat angle = rotation.rotation;            Rotating rotation.view.transform = cgaffinetransformmakerotation (angle); } else if (rotation.state = = uigesturerecognizerstateended) {//restore [UIView animatewithduration:.5 an        imations:^{rotation.view.transform = cgaffinetransformidentity;    }]; }}-(void) Pinchaction: (Uipinchgesturerecognizer *) pinch{if (pinch.state = = Uigesturerecognizerstatechanged) {//take to zoom ratio cgfloat scale = Pinch.scale;            Zoom Pinch.view.transform = Cgaffinetransformmakescale (scales, scale);                        } else if (pinch.state = = uigesturerecognizerstateended) {[UIView animatewithduration:.5 animations:^{        Pinch.view.transform = cgaffinetransformidentity;    }]; }}
Motion Shake gesture
//让当前对象成为第一响应者- (BOOL)canBecomeFirstResponder{             return YES;}- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{   NSLog(@"摇一摇开始");}- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{   NSLog(@"摇一摇结束");}

Recommend a detailed use of iOS gesture Recognition article: iOS gesture recognition

iOS gesture recognizer

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.

Tags Index: