IOS gestures in a detailed

Source: Internet
Author: User

//

Viewcontroller.m

Gesture recognition

//

Created by Cheze on 15/4/2.

Copyright (c) 2015 Cheze. All rights reserved.

//

#import "ViewController.h"

#define ImageFrame CGRectMake (22, 22, 222, 222)

@interface Viewcontroller ()

{

Uiimageview * ImageView;

float rotation;

}

@end

@implementation Viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

ImageView =[[uiimageview Alloc]initwithframe:imageframe];

imageview.tag=10;

Imageview.image=[uiimage imagenamed:@ "1.jpg"];

[ImageView Setuserinteractionenabled:yes];

[Self.view Addsubview:imageview];

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

//

//

[Tap setnumberoftapsrequired:1];//Double-click

[Tap setnumberoftouchesrequired:1];//several fingers

[ImageView addgesturerecognizer:tap];//Add gesture

//

2 Long Press

//

Uilongpressgesturerecognizer * longpress =[[uilongpressgesturerecognizer alloc]initwithtarget:self action:@ Selector (longpress:)];

//

[ImageView addgesturerecognizer:longpress];

//

3 Drag gestures

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

[ImageView Addgesturerecognizer:pan];

//

4 rotation

Uirotationgesturerecognizer * Rotation =[[uirotationgesturerecognizer alloc]initwithtarget:self action: @selector ( Rotation:)];

[ImageView addgesturerecognizer:rotation];

//

5 pinch gesture

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

[ImageView Addgesturerecognizer:pinch];

6 Sweep gestures

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

//

[Swipe setdirection:uiswipegesturerecognizerdirectionright];//gesture direction

[Self.view Addgesturerecognizer:swipe];

Additional setup after loading the view, typically from a nib.

}

-(void) Swipe: (uiswipegesturerecognizer*) Recognier

{

Finger on the screen to drag, sweep gesture interval finger off the screen trigger the listening method

CGRect frame =imageframe;

if (uiswipegesturerecognizerdirectionright==recognier.state) {

Uiimageview * imageview= (uiimageview*) [Self.view viewwithtag:10];

frame.origin.x+=111;

Imageview.frame=frame;

}

}

#pragma mark pinch gesture

-(void) Pinch: (uipinchgesturerecognizer*) recognizer

{

if (uigesturerecognizerstatechanged==recognizer.state) {

[Recognizer.view Settransform:cgaffinetransformmakescale (Recognizer.scale, Recognizer.scale)];

Recognizer.view.transform=cgaffinetransformscale (Recognizer.view.transform, Recognizer.scale, Recognizer.scale);

recognizer.scale=1;

}

}

#pragma mark rotation gesture at least two fingers

-(void) Rotation: (uirotationgesturerecognizer*) recognizer

{

if (uigesturerecognizerstatebegan==recognizer.state) {

Recognizer.view.transform=cgaffinetransformmakerotation (rotation);

NSLog (@ "Start ===%d", recognizer.state);

}

if (uigesturerecognizerstatechanged==recognizer.state) {

Rotation during the change process

Restore at end

1.

[Recognizer.view settransform:cgaffinetransformmakerotation (recognizer.rotation)];

Rotation+=recognizer.rotation;

NSLog (@ "%f in rotation", recognizer.rotation);

NSLog (@ "Rotation =======%f", rotation);

2.

[ImageView settransform:cgaffinetransformrotate (Recognizer.view.transform, recognizer.rotation)];//cumulative deformation The rotation of gesture recognition should be set to 0; the next trigger is based on the current rotation angle.

recognizer.rotation=0;

}else if (uigesturerecognizerstateended==recognizer.state)

{

[Recognizer.view settransform:cgaffinetransformidentity];

}

}

#pragma mark drag gesture

-(void) pan: (uipangesturerecognizer*) recognizer

{

/*

After dragging ends

*/

Cgpoint point = [recognizer TranslationInView:self.view];

NSLog (@ "X:%F; Y:%f ", POINT.X,POINT.Y);

Recognizer.view.center = Cgpointmake (recognizer.view.center.x + point.x, recognizer.view.center.y + point.y);

[Recognizer Settranslation:cgpointmake (0, 0) InView:self.view];

Uigesturerecognizerstatechanged will continue to be called

if (uigesturerecognizerstatechanged==recognizer.state) {

Cgpoint location =[recognizer LocationInView:self.view];

[Recognizer.view setcenter:location];

Cgpoint point = [recognizer TranslationInView:self.view];

[Recognizer.view settransform:cgaffinetransformmaketranslation (Point.x, Point.y)];

[Recognizer.view settransform:cgaffinetransformtranslate (Recognizer.view.transform, Point.x, Point.y)];

//

CGRect Targetrect =imageframe;

Targetrect.origin.x+=point.x;

Targetrect.origin.y+=point.y;

[Recognizer.view Setframe:targetrect];

//    }

else if (uigesturerecognizerstateended==recognizer.state)

//    {

[UIView animatewithduration:1 animations:^{

[Recognizer.view Setframe:imageframe];

//        }];

//    }

}

#pragma Mark Long Press

-(void) Longpress: (uilongpressgesturerecognizer*) recognizer

{

if (uigesturerecognizerstatebegan==recognizer.state)

{

NSLog (@ "long press");

Long-press gesture continuous gesture

[UIView animatewithduration:1 animations:^{

[Recognizer.view settransform:cgaffinetransformmakerotation (M_PI)];

}completion:^ (BOOL finished) {

Restore the view's deformation (pan-zoom rotation)

[Recognizer.view settransform:cgaffinetransformidentity];

}];

}else if (uigesturerecognizerstatechanged==recognizer.state)

{

[UIView animatewithduration:1 animations:^{

[Recognizer.view settransform:cgaffinetransformmakerotation (M_PI)];

//

}completion:^ (BOOL finished) {

Restore the view's deformation (pan-zoom rotation)

[Recognizer.view settransform:cgaffinetransformidentity];

//        }];

}

}

-(void) Tap: (uitapgesturerecognizer*) recognizer

{

NSLog (@ "point me Up");

CGRect Initframe =recognizer.view.frame;

CGRect Targeetframe=recognizer.view.frame;

targeetframe.origin.y+=320;

[UIView animatewithduration:1 animations:^{

[UIView Setanimationrepeatcount:2];

[UIView Setanimationrepeatautoreverses:yes];

[Recognizer.view Setframe:targeetframe];

}completion:^ (BOOL finished) {

[UIView animatewithduration:1 animations:^{

[Recognizer.view Setframe:initframe];

}];

}];

}

-(void) didreceivememorywarning {

[Super didreceivememorywarning];

Dispose of any resources the can be recreated.

}

@end

IOS gestures in a detailed

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.