A summary of various gestures

Source: Internet
Author: User

First, the Viewcontroller to abide by the gesture protocol.

@interface Viewcontroller () <UIGestureRecognizerDelegate>

{

Uiimageview *_imageview;

}

Common gestures

/*

UITapGestureRecognizer Tap (click)

Uipinchgesturerecognizer Pinch (pinch/two fingers inward or outward)

Uirotationgesturerecognizer Rotation (swivel)

Uiswipegesturerecognizer Swipe (sliding, fast moving)

Uipangesturerecognizer Pan (drag, slow move)

Uilongpressgesturerecognizer longpress (Long Press)

*/

Alt/option will appear two fingers in the simulator

-(void) creatview{

_imageview = [[Uiimageview alloc] Initwithframe:cgrectmake (60, 100, 200, 300)];

_imageview.image = [UIImage imagenamed:@ "girl.jpg"];

I want _imageview to interact with the user, like click and rotate, and so on.

The first step must open user interaction

_imageview.userinteractionenabled = YES;

And then you can just add a gesture.

[Self.view Addsubview:_imageview];

}

First, the use of sliding gestures

#pragma mark-Swipe

-(void) CreatGuesture5 {

Swipe gesture 2048

Uiswipegesturerecognizer *swipe = [[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector (Swipeclick :)];

Set direction

Swipe.direction = Uiswipegesturerecognizerdirectionleft;

/*

Uiswipegesturerecognizerdirectionright Swipe Right

Uiswipegesturerecognizerdirectionleft

Uiswipegesturerecognizerdirectionup

Uiswipegesturerecognizerdirectiondown

*/

A gesture can only be set in one direction, there are swipe gestures in all four directions, and four swipe gestures must be created

[_imageview Addgesturerecognizer:swipe];

[Swipe release];

}

-(void) Swipeclick: (Uiswipegesturerecognizer *) Swipe {

NSLog (@ "swipe");

Switch (swipe.direction) {

Case Uiswipegesturerecognizerdirectionleft:

{

NSLog (@ "swipe left");

}

Break

Case Uiswipegesturerecognizerdirectionright:

{

NSLog (@ "right swipe");

}

Break

Case Uiswipegesturerecognizerdirectionup:

{

NSLog (@ "swipe up");

}

Break

Case Uiswipegesturerecognizerdirectiondown:

{

NSLog (@ "swipe down");

}

Break

Default

Break

}

}

2048 slide

#import "RootViewController.h"

@interface Rootviewcontroller ()

@end

@implementation Rootviewcontroller

{

UILabel *_label;

}

-(void) dealloc{

[_label release];

[Super Dealloc];

}

-(ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) Nibbundleornil

{

self = [super Initwithnibname:nibnameornil Bundle:nibbundleornil];

if (self) {

Custom initialization

}

return self;

}

-(void) creatlabel{

_label = [[UILabel alloc] Initwithframe:cgrectmake (110, 100, 100, 100)];

_label.text = @ "2";

_label.layer.maskstobounds = YES;

_label.layer.cornerradius = 5;

_label.textalignment = Nstextalignmentcenter;

_label.backgroundcolor = [Uicolor Redcolor];

[Self.view Addsubview:_label];

}

-(void) viewdidload

{

[Super Viewdidload];

Do any additional setup after loading the view.

[Self Creatlabel];

Uiswipegesturerecognizerdirection Num[4] = {

Uiswipegesturerecognizerdirectionright,

Uiswipegesturerecognizerdirectionleft,

Uiswipegesturerecognizerdirectionup,

Uiswipegesturerecognizerdirectiondown

};

Create a swipe gesture in four directions

for (int i = 0; i < 4; i++) {

Create a swipe gesture

Uiswipegesturerecognizer *swipe = [[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector (swipe:)];

Set gesture Direction

Swipe.direction = Num[i];

Add swipe gestures to the current interface

[Self.view Addgesturerecognizer:swipe];

[Swipe release];

}

}

This function is triggered when the finger slides up or down the screen.

-(void) Swipe: (Uiswipegesturerecognizer *) sw{

Switch (sw.direction) {//direction of gesture

Swipe right

Case Uiswipegesturerecognizerdirectionright:

{

_label.center = Cgpointmake (_label.center.x+100, _LABEL.CENTER.Y);

}

Break

Swipe left

Case Uiswipegesturerecognizerdirectionleft:

{

_label.center = Cgpointmake (_label.center.x-100, _LABEL.CENTER.Y);

}

Break

Swipe up

Case Uiswipegesturerecognizerdirectionup:

{

_label.center = Cgpointmake (_label.center.x, _label.center.y-100);

}

Break

Swipe down

Case Uiswipegesturerecognizerdirectiondown:

{

_label.center = Cgpointmake (_label.center.x, _label.center.y+100);

}

Break

Default

Break

}

}

Second, rotate, pinch, click

#pragma mark-Gesture protocol

Whether to allow two gestures to be valid at the same time (proxy method)

-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: (Uigesturerecognizer *) Othergesturerecognizer {

NSLog (@ "%@-%@", [Gesturerecognizer Class],[othergesturerecognizer class]);

return YES;

}

#pragma mark-rotate gesture

-(void) CreatGuesture3 {

Uirotationgesturerecognizer *rotation = [[Uirotationgesturerecognizer alloc] initwithtarget:self action: @selector ( Rotationclick:)];

Set up the proxy (if you want two gestures to work simultaneously) rotation and kneading are valid simultaneously

Rotation.delegate = self;

[_imageview addgesturerecognizer:rotation];

[Rotation release];

}

Always called during rotation

-(void) Rotationclick: (Uirotationgesturerecognizer *) Rotation {

NSLog (@ "r:%f", rotation.rotation);//gesture Rotation angle

Each time is relative to the current rotation (the current is the reference)

_imageview.transform = Cgaffinetransformrotate (_imageview.transform, rotation.rotation);

The relative radian should be reset to 0

rotation.rotation = 0;

}

#pragma mark-pinch gesture

-(void) CreatGuesture2 {

Pinch gesture two fingers

Uipinchgesturerecognizer *pinch = [[Uipinchgesturerecognizer alloc] initwithtarget:self action: @selector (Pinchclick :)];

Set up the proxy (if you want two gestures to work simultaneously)

Pinch.delegate = self;

[_imageview Addgesturerecognizer:pinch];

[Pinch release];

}

As long as the pinch will always be called

-(void) Pinchclick: (Uipinchgesturerecognizer *) Pinch {

NSLog (@ "kneading%f", Pinch.scale);

Can be scaled

Pinch.scale to get gesture proportions

Equivalent to the most primitive

_imageview.transform = Cgaffinetransformmakescale (Pinch.scale, Pinch.scale);

Relative to the current _imageview.transform

_imageview.transform = Cgaffinetransformscale (_imageview.transform, Pinch.scale, Pinch.scale);

Resetting the gesture ratio to 1 should always be relative.

Pinch.scale = 1;

}

#pragma mark-click gestures

-(void) CreatGuesture1 {

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (Tapclick:)];

Set the number of clicks (1 means click 2 means double-click)

tap.numberoftapsrequired = 2;

Set the number of fingers

tap.numberoftouchesrequired = 1;

Uigesturerecognizer all gestures in the parent class

Add gestures to the picture view

[_imageview Addgesturerecognizer:tap];

[Tap release];

}

-(void) Tapclick: (UITapGestureRecognizer *) Tap {

NSLog (@ "tap gesture");

}

Three, drag, drag gestures

#pragma mark-Move/drag

-(void) CreatGuesture4 {

Uipangesturerecognizer *pan = [[Uipangesturerecognizer alloc] initwithtarget:self action: @selector (Panclick:)];

[_imageview Addgesturerecognizer:pan];

[Pan release];

}

-(void) Panclick: (Uipangesturerecognizer *) Pan {

Gets the offset of the drag gesture relative to the Self.view

Cgpoint curpoint = [Pan TranslationInView:self.view];

NSLog (@ "%@", Nsstringfromcgpoint (Curpoint));

Cgpoint Center = _imageview.center;

Center.x + = Curpoint.x;

Center.y + = Curpoint.y;

_imageview.center = center;

Reset Offset to 0 Cgpointzero = 0 0

[Pan Settranslation:cgpointzero InView:self.view];

}

Four, long press gestures

#pragma mark-long press gestures

-(void) CreatGuesture6 {

Long press DELETE/voice

Uilongpressgesturerecognizer *longpress = [[Uilongpressgesturerecognizer alloc] initwithtarget:self action: @selector (Pressclick:)];

[_imageview addgesturerecognizer:longpress];

[Longpress release];

}

-(void) Pressclick: (Uilongpressgesturerecognizer *) longpress {

NSLog (@ "long press");

Will trigger two times a long press the beginning of the time there is a long press the end of the time

We have to deal with this phenomenon.

Uigesturerecognizerstatebegan//Start

Uigesturerecognizerstateended End

if (longpress.state = = Uigesturerecognizerstatebegan) {

NSLog (@ "Long Press to start");

}

}

A summary of various 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.