IOS Gesture Learning (click, long Press, swipe, drag, rotate, pinch to zoom)

Source: Internet
Author: User

IOS Gesture Learning (click, long Press, swipe, drag, rotate, pinch to zoom)


Click UITapGestureRecognizer

Long press Uilongpressgesturerecognizer

Swipe Uiswipegesturerecognizer

Dragging Uipangesturerecognizer

Rotary Uirotationgesturerecognizer

Pinch to zoom Uipinchgesturerecognizer



The detailed code is as follows:


#import "ViewController.h"

@interface Viewcontroller () <UIGestureRecognizerDelegate>
{
UIView * _view;
}
@end

@implementation Viewcontroller

-(void) Viewdidload {
[Super Viewdidload];

Create a View
_view=[[uiview alloc]initwithframe:cgrectmake (0, 0, 200, 200)];
_view.backgroundcolor=[uicolor Bluecolor];
_view.center=self.view.center;
[Self.view Addsubview:_view];


Click
UITapGestureRecognizer * Tap=[[uitapgesturerecognizer alloc]initwithtarget:self Action: @selector (TapClick)];

Number of clicks currently required
tap.numberoftapsrequired=2;
[Tap Setnumberoftapsrequired:2]; Ditto

Set the hand index required for the current departure time
tap.numberoftouchesrequired=2; Option key to simulate finger
[Tap Setnumberoftouchesrequired:2]; Ditto

[_view Addgesturerecognizer:tap];

Long Press
Uilongpressgesturerecognizer * Longpress=[[uilongpressgesturerecognizer alloc]initwithtarget:self action: @selector (Longpressclick)];

Moving 10 units during a long press also counts as long as the
longpress.allowablemovement=10;
[Longpress Setallowablemovement:10]; Ditto

Min. long press time
longpress.minimumpressduration=2;
[_view addgesturerecognizer:longpress];



Swipe
Uiswipegesturerecognizer * Swipe=[[uiswipegesturerecognizer alloc]initwithtarget:self Action: @selector (swipeAction )];

Direction to the right--
Swipe.direction=uiswipegesturerecognizerdirectionright;
[_view Addgesturerecognizer:swipe];


Swipe
Uiswipegesturerecognizer * Swipe2=[[uiswipegesturerecognizer alloc]initwithtarget:self action: @selector ( SwipeAction2)];

Towards the left, <--.
Swipe2.direction=uiswipegesturerecognizerdirectionleft;
[_view Addgesturerecognizer:swipe2];

Swipe
Uiswipegesturerecognizer * Swipe3=[[uiswipegesturerecognizer alloc]initwithtarget:self action: @selector ( SwipeAction3)];

Towards the top??
Swipe3.direction=uiswipegesturerecognizerdirectionup;
[_view Addgesturerecognizer:swipe3];


Swipe
Uiswipegesturerecognizer * Swipe4=[[uiswipegesturerecognizer alloc]initwithtarget:self action: @selector ( SWIPEACTION4)];

The direction of the downward side??
Swipe4.direction=uiswipegesturerecognizerdirectiondown;
[_view Addgesturerecognizer:swipe4];

Dragging
Uipangesturerecognizer * Pan=[[uipangesturerecognizer alloc]initwithtarget:self Action: @selector (panAction:)];

[_view Addgesturerecognizer:pan];


Rotating
Uirotationgesturerecognizer * Rotation=[[uirotationgesturerecognizer alloc]initwithtarget:self action: @selector ( Rotation:)];
rotation.delegate=self;
[_view addgesturerecognizer:rotation];


Pinch to zoom
Uipinchgesturerecognizer * Pinch=[[uipinchgesturerecognizer alloc]initwithtarget:self action: @selector (pinch:)];
pinch.delegate=self;
[_view Addgesturerecognizer:pinch];

}


#pragma mark-click
-(void) Tapclick
{
NSLog (@ "click");
}
#pragma mark-long press
-(void) Longpressclick
{
NSLog (@ "long press");
}

#pragma mark-Slide right
-(void) swipeaction
{
NSLog (@ "right");
}
#pragma mark-Slide left
-(void) SwipeAction2
{
NSLog (@ "left");
}
#pragma mark-Slide up
-(void) SwipeAction3
{
NSLog (@ "up");
}
#pragma mark-slipped
-(void) SwipeAction4
{
NSLog (@ "down");
}

#pragma mark-drag-and-drop movement
-(void) Panaction: (Uipangesturerecognizer *) pan
{
Get the size of the move
Cgpoint Point=[pan TranslationInView:pan.view];

Change the center point coordinates of a view
Cgpoint Points=_view.center;
Points.x+=point.x;
Points.y+=point.y;
_view.center=points;

Clear each time, eliminate the coordinate overlay
[Pan Settranslation:cgpointzero InView:pan.view];
}

#pragma mark-Rotate
-(void) Rotation: (Uirotationgesturerecognizer *) rote
{
Gets the degree of the current rotation
CGFloat rotation=rote.rotation;

Rotation through affine transformations
_view.transform=cgaffinetransformrotate (_view.transform, rotation);
Prevent rotation overlay, clear zero
rote.rotation=0;

}

#pragma mark-Zoom pinch
-(void) Pinch: (Uipinchgesturerecognizer *) pinch
{
Get scale
CGFloat Scale=pinch.scale;

Scaling with affine transformations
_view.transform=cgaffinetransformscale (_view.transform, scale, scale);

Prevent scale Overlay
pinch.scale=1;


}

#pragma the mark-proxy method enables rotation + zoom kneading to be performed simultaneously
-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: (Uigesturerecognizer *) Othergesturerecognizer
{
return YES;
}


-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}

@end



Points to note:

1. The option key on the keyboard simulates the finger


2. for the same view , when dragged, the left-to-right upward downward gesture fails.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

IOS gesture Learning (tap, long press, swipe, drag, rotate, pinch to zoom)

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.