Created
December 15, 2011 03:33
-
-
Save linktoming/1479712 to your computer and use it in GitHub Desktop.
Gesture Recognition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************** | |
* Add Recognizer to view | |
* | |
* UITapGestureRecognizer | |
* UISwipeGestureRecognizer | |
* UIPinchGestureRecognizer | |
* UIRotationGestureRecognizer | |
* UIPanGestureRecognizer | |
* UILongPressGestureRecognizer | |
*/ | |
// For tap gesture | |
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)]; | |
[tapRecognizer setNumberOfTapsRequired:1]; | |
// [tapRecognizer requireGestureRecognizerToFail:doubleTap]; // For resolve gesture confliction | |
// [tapRecognizer setNumberOfTouchesRequired:2]; | |
// [tapRecognizer setDelegate:self]; // for web view | |
[holderView addGestureRecognizer:tapRecognizer]; | |
[tapRecognizer release]; | |
// For swipe gesture | |
UISwipeGestureRecognizer* swipeLeftOnMainView = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeOnMainView:)]; | |
swipeLeftOnMainView.direction = UISwipeGestureRecognizerDirectionLeft; | |
[self.mainView addGestureRecognizer:swipeLeftOnMainView]; | |
[swipeLeftOnMainView release]; | |
// For two finger pinch | |
UIPinchGestureRecognizer *twoFingerPinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)]; | |
[[self view] addGestureRecognizer:twoFingerPinch]; | |
[twoFingerPinch release]; | |
// For two finger rotate | |
UIRotationGestureRecognizer *twoFingersRotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersRotate:)]; | |
[[self view] addGestureRecognizer:twoFingersRotate]; | |
[twoFingersRotate release]; | |
/******************************************************************** | |
* Gesture Recognizer Action Methods | |
* - (void)handleGesture | |
* - (void)handleGesture:(UIGestureRecognizer *)sender | |
*/ | |
- (void)oneFingerTwoTaps | |
{ | |
NSLog(@"Action: One finger, two taps"); | |
} | |
- (void)oneFingerSwipeUp:(UISwipeGestureRecognizer *)recognizer | |
{ | |
CGPoint point = [recognizer locationInView:[self view]]; | |
NSLog(@"Swipe up - start location: %f,%f", point.x, point.y); | |
} | |
- (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)); | |
} | |
- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer | |
{ | |
NSLog(@"Pinch scale: %f", recognizer.scale); | |
} | |
/******************************************************************** | |
* - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer | |
* - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch | |
* - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer | |
* shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer | |
* | |
* For add gesture recognizer to UIWebView, we need to set delegate | |
* for it so that it'll recognize gestures simultaneously | |
*/ | |
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ | |
return YES; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment