Basic gestures for iOS

Source: Internet
Author: User

1. Tap gestures:

UITapGestureRecognizer *tapgesture = [[UITapGestureRecognizer alloc]initwithtarget:self Action: @selector ( Handletapgesture:)];
Set the click order to use when recognizing gestures
tapgesture.numberoftapsrequired = 2;

2. Long-Press gestures:

Uilongpressgesturerecognizer *longpress = [[Uilongpressgesturerecognizer alloc]initwithtarget:self Action: @selector (handlelongpressgesture:)];
Set press minimum duration, default 0.5s
Longpress.minimumpressduration = 1;

3. Swipe gestures:

Uipangesturerecognizer *pangesture = [[Uipangesturerecognizer alloc]initwithtarget:self Action: @selector ( Handlepangesture:)];
[View Addgesturerecognizer:pangesture];

4. Pinch gesture:

Uipinchgesturerecognizer *pinchgesture = [[Uipinchgesturerecognizer alloc]initwithtarget:self Action: @selector ( Handlepinchgesture:)];
Pinchgesture.delegate = self;
5. Rotating gestures:

Uirotationgesturerecognizer *rotationgesture = [[Uirotationgesturerecognizer alloc]initwithtarget:self action:@ Selector (handlerotationgesture:)];
Rotationgesture.delegate = self;
#pragma mark-
#pragma mark Handle Gesture Recoginzer

-(void) Handlerotationgesture: (uirotationgesturerecognizer*) gesture{
if (gesture.state = = Uigesturerecognizerstatebegan | | gesture.state = = uigesturerecognizerstatechanged) {
UIView *view = Gesture.view;
[Self.view Bringsubviewtofront:view];
The radian of the rotation
CGFloat rotation = gesture.rotation;
View.transform = Cgaffinetransformrotate (view.transform, rotation);

Note that there is an accumulation
gesture.rotation = 0.0;

-(void) Handlepinchgesture: (uipinchgesturerecognizer*) gesture
if (gesture.state = = Uigesturerecognizerstatebegan | | gesture.state = = uigesturerecognizerstatechanged) {
UIView *view = Gesture.view;
[Self.view Bringsubviewtofront:view];
Get scaled scale, then set the scaling parameters of the transform
Gesture.scale Zoom ratio when kneading
CGFloat scale = Gesture.scale;

Set View.transform scale to represent scaling
The first parameter, which is the base value, is scaled on this basis
The second parameter, the scale of the x-coordinate direction
The third parameter, the scale of the y-coordinate direction
View.transform = Cgaffinetransformscale (view.transform, scale, scale);

As the gesture effect accumulates, you need to reset it.
Gesture.scale = 1.0;

-(void) Handlepangesture: (uipangesturerecognizer*) gesture
if (gesture.state = = Uigesturerecognizerstatebegan | | gesture.state = = uigesturerecognizerstatechanged) {
Gesture recognition is successful, and gestures continue in progress
UIView *view = Gesture.view;
[Self.view Bringsubviewtofront:view];
How much the gesture moves on the Self.view
Cgpoint offset = [gesture TranslationInView:self.view];
Cgpoint newcenter = Cgpointmake (, VIEW.CENTER.Y+OFFSET.Y); = Newcenter;

The effect of the gesture will accumulate, translation always accumulate
Re-initialize to remove the original cumulative effect
[Gesture Settranslation:cgpointzero InView:self.view];

Click gesture Processing
-(void) Handletapgesture: (uitapgesturerecognizer*) gesture
Gesture.view; Represents which view the gesture occurred on
NSLog (@ "Clicked 2 times, gesture recognition succeeded");

Gesture recognition process, first accept the event to Touchbegin, start gesture recognition, gesture status
Uigesturerecognizerstatepossible, then continues to receive and analyze touch events, and if gesture recognition succeeds, the status of the gesture becomes Uigesturerecognizerstatebegan
If the gesture continues, the state of the gesture changes to uigesturerecognizerstatechanged
If the gesture ends, the status changes to Uigesturerecognizerstateended
If gesture recognition fails uigesturerecognizerstatefailed
-(void) Handlelongpressgesture: (uilongpressgesturerecognizer*) gestrue
if (gestrue.state = = Uigesturerecognizerstatebegan | | gestrue.state = = uigesturerecognizerstatechanged) {
NSLog (@ "Long press gesture recognition succeeded");
if (gestrue.state = = uigesturerecognizerstateended) {
NSLog (@ "End of gesture recognition");

#pragma mark-
#pragma Mark Uigesturereconigerdelegate
-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: (Uigesturerecognizer *) Othergesturerecognizer
if (Gesturerecognizer.view = = Othergesturerecognizer.view) {
return YES;
return NO;

Basic gestures for iOS

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