So, Apple tries to make our developer life easier to breath by providing us a built-in mechanism to detect these common gestures on the latest iPhone SDK version.
You can find these APIs from UIGestureRecognizer class.
"A gesture-recognizer object (or, simply, a gesture recognizer) decouples the logic for recognizing a gesture and acting on that recognition. When one of these objects recognizes a common gesture or, in some cases, a change in the gesture, it sends an action message to each designated target object" - from Apple API documentation.
This class derives to:
1. UITapGestureRecognizer: presents the gesture that p fingers tapped the screen q times consecutively.
- Input properties: numberOfTaps (q), numberOfFingers (p)
- Output properties: location, touches
3. UIRotationGestureRecognizer: looks for rotation gestures involving two touches. When the user moves the fingers opposite each other in a circular motion, the underlying view should rotate in a corresponding direction and speed.
4. UIPinchGestureRecognizer: represents the gesture that 2 fingers moving towards or away from a center point. This gesture is usually used for scaling a view.
- Input properties: scaleThreshold
- Output properties: scale, velocity, anchorPoint, transform
5. UILongPressGestureRecognizer: represents the gesture that p fingers moved/stayed on the screen continuously for T seconds.
- Input properties: numberOfFingers (p), delay (T), allowableMovement
- Output properties: touches, centroid, startPoint
- Input properties: minimumDistance (d), angle (θ), maximumDeviation (Δθ), restrictsToAngle
- Output properties: touch, startPosition, startAngle
7. UIPanGestureRecognizer: represents the gesture that 1 or more fingers moving on the screen for a relatively large distance. This gesture is usually used for scrolling, and is used by UIScroller and UIScrollView.
- Input properties: directionalLockEnabled
- Output properties: offset, velocity, transform
- Input properties: numberOfFullTaps (q), allowableMovement
- Output properties: touch
An example for how to use these API:
UISwipeGestureRecognizer *swipeRecognizer = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(DidSwipe:)] autorelease];
By default this line of code detect the swipes from left to right.
If you want to detect a swipe from right to left add the below line after the above line:
swipeRecognizer
.direction = UISwipeGestureRecognizerDirectionLeft;
On 1 view, you can add many gesture recognizers to handle different interactions by using below function
[self.view addGestureRecognizer:
swipeRecognizer
];
After that, just implement the [
DidSwipe
] method we already registered above:- (void)
DidSwipe
:(UISwipeGestureRecognizer*)sender{
Do something...
}
Refs:
+ http://iphonedevwiki.net/index.php/UIGestureRecognizer
+ iPhone SDK 3.2 API document
Happy coding!