IOS Beginner-gestures

Source: Internet
Author: User

I. To learn gestures, first set up an object to operate, we take a photo as an example. First create a ImageView

1. Declare a property:

@property (nonatomic, retain) Uiimageview *imageview;

2. Start a Real object creation

Self.imageview = [[Uiimageview alloc]initwithimage:[uiimage imagenamed:@ "beautiful.jpg"]];

Self.imageView.frame = CGRectMake (100, 200, 200, 300);

[Self.view AddSubview:self.imageView];

[_imageview release];

3. If you want to create a view, open the Picture user interaction feature, by default, no

self.imageView.userInteractionEnabled = YES;

Two, a few common methods of gesture

1. Click events

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (tapaction:)];// Initialize method, add click Method named Tapaction:

[Self.imageview addgesturerecognizer:tap];//adds gestures to the specified view

[Tap release];

tap.numberoftouchesrequired = 2;//set several finger taps to respond

tap.numberoftapsrequired = 2;//Set a few consecutive clicks to respond

click Method

-(void) Tapaction: (UITapGestureRecognizer *) tap

{

NSLog (@ "I was clicked");

}

2. Long Press Events

Uilongpressgesturerecognizer *longpress = [[Uilongpressgesturerecognizer alloc]initwithtarget:self Action: @selector (longpressaction:)];//the same add event with the event named Longpressaction:

[Self.imageview addgesturerecognizer:longpress];//to add gestures to ImageView

[Longpress release];

Longpress.minimumpressduration = 1;//Set long press trigger time

-(void) Longpressaction: (UITapGestureRecognizer *) longpressaction

{

if (longpressaction.state = = Uigesturerecognizerstatebegan) {

NSLog (@ "Long Press to start");

}

}

3. Rotate

Uirotationgesturerecognizer *rotation = [[Uirotationgesturerecognizer alloc]initwithtarget:self Action: @selector ( Rotationaction:)];

[Self.imageview addgesturerecognizer:rotation];

[Rotation release];

The above method of creation is the previous two methods.

-(void) Rotationaction: (Uirotationgesturerecognizer *) rotationaction{

You can use gestures to know that gestures are added to that view

Uiimageview *imageview = (Uiimageview *) Rotationaction.view;

Find the view and let the view rotate

Imageview.transform = Cgaffinetransformrotate (Imageview.transform, rotationaction.rotation);

rotationaction.rotation = 0;

}

4. Zoom function

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

[Self.imageview Addgesturerecognizer:pinch];

[Pinch release];

-(void) Pinch: (Uipinchgesturerecognizer *) pinch

{

Find a view with gestures

Uiimageview *imageviews = (Uiimageview *) Pinch.view;

Find the view to zoom in or zoom in on the view

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

Pinch.scale = 1;

}

5. Drag and drop gestures

Uipangesturerecognizer *tuozhuai = [[Uipangesturerecognizer alloc] initwithtarget:self action: @selector (Tuozhuai:)];

[Self.imageview Addgesturerecognizer:tuozhuai];

[Tuozhuai release];

-(void) Tuozhuai: (Uipangesturerecognizer *) Tuozhuai

{

Find a view with gestures

Uiimageview *imageview = (Uiimageview *) Tuozhuai.view;

Use gestures to get moving points

Cgpoint p = [Tuozhuai Translationinview:imageview];

Cgpoint p = [Tuozhuai Trans:imageview];

Drag and drop via position transform

Imageview.transform = Cgaffinetransformtranslate (Imageview.transform, p.x, P.Y);

[Tuozhuai Settranslation:cgpointzero Inview:imageview];

}

6. Sweep gesture, this gesture is to look at your sliding direction

Uiswipegesturerecognizer *qingsao = [[Uiswipegesturerecognizer alloc]initwithtarget:self Action: @selector (Qingsao:) ];

[Self.view Addgesturerecognizer:qingsao];

[Qingsao release];

Qingsao.direction = Direction of uiswipegesturerecognizerdirectionleft;//sweep

-(void) Qingsao: (Uiswipegesturerecognizer *) Qingsao

{

if (qingsao.direction = = Uiswipegesturerecognizerdirectionleft) {

NSLog (@ "sweep to the Left");

}

}

IOS Beginner-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.