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 );
}