Code snippet of common IOS gestures

Source: Internet
Author: User
Tags in degrees

IOS gesture code snippet to http://www.oschina.net/code/snippet_54100_8106

The uikit contains the uigesturerecognizer class, which is used to detect gestures that occur on the device. Uigesturerecognizer is an abstract class that defines the basic behavior of all gestures. It has the following sub-classes used to process specific gestures:

1. Click uitapgesturerecognizer (any number of hits)
2. Push uipinchgesturerecognizer inside or outside (used for scaling)
3. Shake or drag uipangesturerecognizer
4. Touch uiswipegesturerecognizer (in any direction)
5. Rotate uirotationgesturerecognizer (the finger moves in the opposite direction)
6. uilongpressgesturerecognizer

Different types of gesture identifiers have different configuration attributes. For example, uitapgesturerecognizer can configure the number of hits. After receiving a gesture, the interface can send a message to process the task after the gesture action is returned. Of course, different gesture identifiers send different message methods. The following code is an example:

[Code] One finger, two clap gestures
// Create a gesture identification device
Uitapgesturerecognizer * onefingertwotaps =
[[Uitapgesturerecognizer alloc] initwithtarget: Self action: @ selector (onefingertwotaps)] autorelzer];
// Set required taps and number of touches
[Onefingertwotaps setnumberoftapsrequired: 2];
[Onefingertwotaps setnumberoftouchesrequired: 1];
// Add the gesture to the view
[[Self view] addgesturerecognizer: onefingertwotaps];

Message method onefingertwotaps
-(Void) onefingertwotaps
{
Nslog (@ "Action: one finger, two taps ");
}
[Code] two fingers, two click gestures
Uitapgesturerecognizer * twofingerstwotaps =
[[Uitapgesturerecognizer alloc] initwithtarget: Self action: @ selector (twofingerstwotaps)] autorelaps];
[Twofingerstwotaps setnumberoftapsrequired: 2];
[Twofingerstwotaps setnumberoftouchesrequired: 2];
[[Self view] addgesturerecognizer: twofingerstwotaps];

Message method twofingerstwotaps

-(Void) twofingerstwotaps {
Nslog (@ "Action: two fingers, two taps ");
}
[Code] a gesture of rubbing your finger up or down
// Touch up
Uiswipegesturerecognizer * onefingerswipeup =
[[Uiswipegesturerecognizer alloc] initwithtarget: Self action: @ selector (onefingerswipeup :)] autorelup];
[Onefingerswipeup setdirection: uiswipegesturerecognizerdireup up];
[[Self view] addgesturerecognizer: onefingerswipeup];

-(Void) onefingerswipeup :( uiswipegesturerecognizer *) recognizer
{
Cgpoint point = [recognizer locationinview: [self view];
Nslog (@ "swipe up-start location: % F, % F", point. X, point. y );
}

// Touch down

Uiswipegesturerecognizer * onefingerswipedown =

[[Uiswipegesturerecognizer alloc] initwithtarget: Self action: @ selector (onefingerswipedown :)] autoreldown];

[Onefingerswipedown setdirection: uiswipegesturerecognizerdirectiondown];

[[Self view] addgesturerecognizer: onefingerswipedown];

-(Void) onefingerswipedown :( uiswipegesturerecognizer *) recognizer

{

Cgpoint point = [recognizer locationinview: [self view];

Nslog (@ "swipe down-start location: % F, % F", point. X, point. y );

}
[Code] rotation gesture

Uirotationgesturerecognizer * twofingersrotate =

[[[Uirotationgesturerecognizer alloc] initwithtarget: Self action: @ selector (twofingersrotate :)] autorelease];

[[Self view] addgesturerecognizer: twofingersrotate];

-(Void) twofingersrotate :( uirotationgesturerecognizer *) recognizer

{

// Convert the radian value to show the degree of Rotation

Nslog (@ "rotation in degrees since last change: % F", [recognizer rotation] * (180/m_pi ));

}
[Code] gesture of inside or outside
View sourceprint?

Uipinchgesturerecognizer * twofingerpinch =

[[Uipinchgesturerecognizer alloc] initwithtarget: Self action: @ selector (twofingerpinch :)] autorelease];

[[Self view] addgesturerecognizer: twofingerpinch];

-(Void) twofingerpinch :( uipinchgesturerecognizer *) recognizer

{

Nslog (@ "pinch scale: % F", recognizer. scale );
}

Related Article

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.