文章出處

1、UIGestureRecognizer介紹

手勢識別在iOS上非常重要,手勢操作移動設備的重要特征,極大的增加了移動設備使用便捷性。

iOS系統在3.2以后,為方便開發這使用一些常用的手勢,提供了UIGestureRecognizer類。手勢識別UIGestureRecognizer類是個抽象類,下面的子類是具體的手勢,開發這可以直接使用這些手勢識別。

UITapGestureRecognizer 

UIPinchGestureRecognizer 

UIRotationGestureRecognizer 

UISwipeGestureRecognizer 

UIPanGestureRecognizer 

UILongPressGestureRecognizer



上面的手勢對應的操作是:
  • Tap(點一下)
  • Pinch(二指往內或往外撥動,平時經常用到的縮放)
  • Rotation(旋轉)
  • Swipe(滑動,快速移動)
  • Pan (拖移,慢速移動)
  • LongPress(長按)
UIGestureRecognizer的繼承關系如下:


<ignore_js_op>1361893916_7845.png 


2、使用手勢的步驟

使用手勢很簡單,分為兩步:

  • 創建手勢實例。當創建手勢時,指定一個回調方法,當手勢開始,改變、或結束時,回調方法被調用。
  • 添加到需要識別的View中。每個手勢只對應一個View,當屏幕觸摸在View的邊界內時,如果手勢和預定的一樣,那就會回調方法。

ps:一個手勢只能對應一個View,但是一個View可以有多個手勢。

建議在真機上運行這些手勢,模擬器操作不太方便,可能導致你認為手勢失效。

3、Pan 拖動手勢:

[cpp] view plaincopyprint?

  1. UIImageView *snakeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"snake.png"]]; 
  2. snakeImageView.frame = CGRectMake(50, 50, 100, 160); 
  3. UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] 
  4. initWithTarget:self 
  5. action:@selector(handlePan:)]; 
  6. [snakeImageView addGestureRecognizer:panGestureRecognizer]; 
  7. [self.view setBackgroundColor:[UIColor whiteColor]]; 
  8. [self.view addSubview:snakeImageView]; 
  9. UIImageView *snakeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"snake.png"]];
  10. snakeImageView.frame = CGRectMake(50, 50, 100, 160);
  11. UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
  12. initWithTarget:self
  13. action:@selector(handlePan:)]; 
  14. [snakeImageView addGestureRecognizer:panGestureRecognizer];
  15. [self.view setBackgroundColor:[UIColor whiteColor]];
  16. [self.view addSubview:snakeImageView];
復制代碼

新建一個ImageView,然后添加手勢

回調方法:

[cpp] view plaincopyprint?
  1. - (void) handlePan:(UIPanGestureRecognizer*) recognizer 
  2. CGPoint translation = [recognizer translationInView:self.view]; 
  3. recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, 
  4. recognizer.view.center.y + translation.y); 
  5. [recognizer setTranslation:CGPointZero inView:self.view]; 
  6. - (void) handlePan:(UIPanGestureRecognizer*) recognizer
  7. {
  8. CGPoint translation = [recognizer translationInView:self.view];
  9. recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
  10. recognizer.view.center.y + translation.y);
  11. [recognizer setTranslation:CGPointZero inView:self.view];
  12. }
復制代碼

  • 4、Pinch縮放手勢
[cpp] view plaincopyprint?
  1. UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] 
  2. initWithTarget:self 
  3. action:@selector(handlePinch:)];<P class="p1">[<SPAN class="s1">snakeImageView</SPAN> <SPAN class="s2">addGestureRecognizer</SPAN>:pinchGestureRecognizer];</P> 
  4. UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc]
  5. initWithTarget:self
  6. action:@selector(handlePinch:)];[snakeImageView addGestureRecognizer:pinchGestureRecognizer];
復制代碼

[cpp] view plaincopyprint?
  1. - (void) handlePinch:(UIPinchGestureRecognizer*) recognizer 
  2. recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale); 
  3. recognizer.scale = 1; 
  4. - (void) handlePinch:(UIPinchGestureRecognizer*) recognizer
  5. {
  6. recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
  7. recognizer.scale = 1;
  8. }
復制代碼

5、Rotation旋轉手勢

[cpp] view plaincopyprint?
  1. UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer alloc] 
  2. initWithTarget:self 
  3. action:@selector(handleRotate:)]; 
  4. [snakeImageView addGestureRecognizer:rotateRecognizer]; 
  5. UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer alloc]
  6. initWithTarget:self
  7. action:@selector(handleRotate:)];
  8. [snakeImageView addGestureRecognizer:rotateRecognizer];
復制代碼

[cpp] view plaincopyprint?


  1. - (void) handleRotate:(UIRotationGestureRecognizer*) recognizer 
  2. recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation); 
  3. recognizer.rotation = 0; 
  4. - (void) handleRotate:(UIRotationGestureRecognizer*) recognizer
  5. {
  6. recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
  7. recognizer.rotation = 0;
  8. }
復制代碼



1361953288_8283.png 


添加了這幾個手勢后,運行看效果,程序中的imageView放了一個

/^\/^\
_|__| O|
\/ /~ \_/ \
\____|__________/ \
\_______ \
`\ \ \
| | \
/ / \
/ / \\
/ / \ \
/ / \ \
/ / _----_ \ \
/ / _-~ ~-_ | |
( ( _-~ _--_ ~-_ _/ |
\ ~-____-~ _-~ ~-_ ~-_-~ /
~-_ _-~ ~-_ _-~ 
~--______-~ ~-___-~

的圖片,在模擬器上拖動是沒問題的。縮放和旋轉有點問題,估計是因為在模擬器上的模擬的兩個接觸點距離在imageView的邊界外了,所以操作無效果。

建議在真機上運行這個手勢。

在模擬器上縮放和選擇的操作技巧:

可以把imageView的frame值設置大一點,按住alt鍵,按下觸摸板(不按下不行),這樣就可以旋轉和縮放了。

6、添加第二個ImagView并添加手勢

記住:一個手勢只能添加到一個View,兩個View當然要有兩個手勢的實例了

[cpp] view plaincopyprint?

  1. - (void)viewDidLoad 
  2. [super viewDidLoad]; 
  3. UIImageView *snakeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"snake.png"]]; 
  4. UIImageView *dragonImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dragon.png"]]; 
  5. snakeImageView.frame = CGRectMake(120, 120, 100, 160); 
  6. dragonImageView.frame = CGRectMake(50, 50, 100, 160); 
  7. [self.view addSubview:snakeImageView]; 
  8. [self.view addSubview:dragonImageView]; 
  9. for (UIView *view in self.view.subviews) { 
  10. UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] 
  11. initWithTarget:self 
  12. action:@selector(handlePan:)]; 
  13. UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] 
  14. initWithTarget:self 
  15. action:@selector(handlePinch:)]; 
  16. UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer alloc] 
  17. initWithTarget:self 
  18. action:@selector(handleRotate:)]; 
  19. [view addGestureRecognizer:panGestureRecognizer]; 
  20. [view addGestureRecognizer:pinchGestureRecognizer]; 
  21. [view addGestureRecognizer:rotateRecognizer]; 
  22. [view setUserInteractionEnabled:YES]; 
  23. [self.view setBackgroundColor:[UIColor whiteColor]]; 
  24. - (void)viewDidLoad
  25. {
  26. [super viewDidLoad];
  27. UIImageView *snakeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"snake.png"]];
  28. UIImageView *dragonImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dragon.png"]];
  29. snakeImageView.frame = CGRectMake(120, 120, 100, 160);
  30. dragonImageView.frame = CGRectMake(50, 50, 100, 160);
  31. [self.view addSubview:snakeImageView];
  32. [self.view addSubview:dragonImageView];
  33. for (UIView *view in self.view.subviews) {
  34. UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
  35. initWithTarget:self
  36. action:@selector(handlePan:)];
  37. UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc]
  38. initWithTarget:self
  39. action:@selector(handlePinch:)];
  40. UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer alloc]
  41. initWithTarget:self
  42. action:@selector(handleRotate:)];
  43. [view addGestureRecognizer:panGestureRecognizer];
  44. [view addGestureRecognizer:pinchGestureRecognizer];
  45. [view addGestureRecognizer:rotateRecognizer];
  46. [view setUserInteractionEnabled:YES];
  47. }
  48. [self.view setBackgroundColor:[UIColor whiteColor]]; 
  49. }
復制代碼

多添加了一條龍的view,兩個view都能接收上面的三種手勢。運行效果如下:

<ignore_js_op>1361954419_1036.png 


7、拖動(pan手勢)速度(以較快的速度拖放后view有滑行的效果)

如何實現呢?

  • 監視手勢是否結束
  • 監視觸摸的速度
[cpp] view plaincopyprint?

  1. - (void) handlePan:(UIPanGestureRecognizer*) recognizer 
  2. CGPoint translation = [recognizer translationInView:self.view]; 
  3. recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, 
  4. recognizer.view.center.y + translation.y); 
  5. [recognizer setTranslation:CGPointZero inView:self.view]; 
  6. if (recognizer.state == UIGestureRecognizerStateEnded) { 
  7. CGPoint velocity = [recognizer velocityInView:self.view]; 
  8. CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y)); 
  9. CGFloat slideMult = magnitude / 200; 
  10. NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult); 
  11. float slideFactor = 0.1 * slideMult; // Increase for more of a slide 
  12. CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor), 
  13. recognizer.view.center.y + (velocity.y * slideFactor)); 
  14. finalPoint.x = MIN(MAX(finalPoint.x, 0), self.view.bounds.size.width); 
  15. finalPoint.y = MIN(MAX(finalPoint.y, 0), self.view.bounds.size.height); 
  16. [UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ 
  17. recognizer.view.center = finalPoint; 
  18. } completion:nil]; 
  19. - (void) handlePan:(UIPanGestureRecognizer*) recognizer
  20. {
  21. CGPoint translation = [recognizer translationInView:self.view];
  22. recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
  23. recognizer.view.center.y + translation.y);
  24. [recognizer setTranslation:CGPointZero inView:self.view];
  25. if (recognizer.state == UIGestureRecognizerStateEnded) {
  26. CGPoint velocity = [recognizer velocityInView:self.view];
  27. CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
  28. CGFloat slideMult = magnitude / 200;
  29. NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult);
  30. float slideFactor = 0.1 * slideMult; // Increase for more of a slide
  31. CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor),
  32. recognizer.view.center.y + (velocity.y * slideFactor));
  33. finalPoint.x = MIN(MAX(finalPoint.x, 0), self.view.bounds.size.width);
  34. finalPoint.y = MIN(MAX(finalPoint.y, 0), self.view.bounds.size.height);
  35. [UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
  36. recognizer.view.center = finalPoint;
  37. } completion:nil];
  38. }
復制代碼

   代碼實現解析:

  • 計算速度向量的長度(估計大部分都忘了)這些知識了。
  • 如果速度向量小于200,那就會得到一個小于的小數,那么滑行會很短
  • 基于速度和速度因素計算一個終點
  • 確保終點不會跑出父View的邊界
  • 使用UIView動畫使view滑動到終點
運行后,快速拖動圖像view放開會看到view還會在原來的方向滑行一段路。

8、同時觸發兩個view的手勢

手勢之間是互斥的,如果你想同時觸發蛇和龍的view,那么需要實現協議 

UIGestureRecognizerDelegate,


[cpp] view plaincopyprint?
  1. @interface ViewController : UIViewController<UIGestureRecognizerDelegate> 
  2. @end 
  3. @interface ViewController : UIViewController<UIGestureRecognizerDelegate>
  4. @end
復制代碼

并在協議這個方法里返回YES。 

[cpp] view plaincopyprint?
  1. -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 
  2. return YES; 
  3. -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
  4. {
  5. return YES;
  6. }
復制代碼
把self作為代理設置給手勢: 

[cpp] view plaincopyprint?
  1. panGestureRecognizer.delegate = self; 
  2. pinchGestureRecognizer.delegate = self; 
  3. rotateRecognizer.delegate = self; 
  4. panGestureRecognizer.delegate = self;
  5. pinchGestureRecognizer.delegate = self;
  6. rotateRecognizer.delegate = self;
復制代碼

這樣可以同時拖動或旋轉縮放兩個view了。 

9、tap點擊手勢

這里為了方便看到tap的效果,當點擊一下屏幕時,播放一個聲音。


為了播放聲音,我們加入AVFoundation.framework這個框架。


[cpp] view plaincopyprint?
  1. - (AVAudioPlayer *)loadWav:(NSString *)filename { 
  2. NSURL * url = [[NSBundle mainBundle] URLForResource:filename withExtension:@"wav"]; 
  3. NSError * error; 
  4. AVAudioPlayer * player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; 
  5. if (!player) { 
  6. NSLog(@"Error loading %@: %@", url, error.localizedDescription); 
  7. } else { 
  8. [player prepareToPlay]; 
  9. return player; 
  10. - (AVAudioPlayer *)loadWav:(NSString *)filename {
  11. NSURL * url = [[NSBundle mainBundle] URLForResource:filename withExtension:@"wav"];
  12. NSError * error;
  13. AVAudioPlayer * player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
  14. if (!player) {
  15. NSLog(@"Error loading %@: %@", url, error.localizedDescription);
  16. } else {
  17. [player prepareToPlay];
  18. }
  19. return player;
  20. }
復制代碼

我會在最后例子代碼給出完整代碼,添加手勢的步驟和前面一樣的。 

[cpp] view plaincopyprint?
  1. #import <UIKit/UIKit.h> 
  2. #import <AVFoundation/AVFoundation.h> 
  3. @interface ViewController : UIViewController<UIGestureRecognizerDelegate> 
  4. @property (strong) AVAudioPlayer * chompPlayer; 
  5. @property (strong) AVAudioPlayer * hehePlayer; 
  6. @end 
  7. #import <UIKit/UIKit.h>
  8. #import <AVFoundation/AVFoundation.h>
  9. @interface ViewController : UIViewController<UIGestureRecognizerDelegate>
  10. @property (strong) AVAudioPlayer * chompPlayer;
  11. @property (strong) AVAudioPlayer * hehePlayer;
  12. @end
復制代碼

[cpp] view plaincopyprint?
  1. - (void)handleTap:(UITapGestureRecognizer *)recognizer { 
  2. [self.chompPlayer play]; 
  3. - (void)handleTap:(UITapGestureRecognizer *)recognizer {
  4. [self.chompPlayer play];
  5. }
復制代碼

運行,點一下某個圖,就會播放一個咬東西的聲音。


不過這個點擊播放聲音有點缺陷,就是在慢慢拖動的時候也會播放。這使得兩個手勢重合了。怎么解決呢?使用手勢的:requireGestureRecognizerToFail方法。


10、手勢的依賴性

在viewDidLoad的循環里添加這段代碼:

[cpp] view plaincopyprint?
  1. [tapRecognizer requireGestureRecognizerToFail:panGestureRecognizer]; 
  2. [tapRecognizer requireGestureRecognizerToFail:panGestureRecognizer];
復制代碼

意思就是,當如果pan手勢失敗,就是沒發生拖動,才會出發tap手勢。這樣如果你有輕微的拖動,那就是pan手勢發生了。tap的聲音就不會發出來了。 

11、自定義手勢


自定義手勢繼承:UIGestureRecognizer,實現下面的方法:


[cpp] view plaincopyprint?
  1. – touchesBegan:withEvent: 
  2. – touchesMoved:withEvent: 
  3. – touchesEnded:withEvent: 
  4. - touchesCancelled:withEvent: 
  5. – touchesBegan:withEvent:
  6. – touchesMoved:withEvent:
  7. – touchesEnded:withEvent:
  8. - touchesCancelled:withEvent:
復制代碼

新建一個類,繼承UIGestureRecognizer,代碼如下:


.h文件


[cpp] view plaincopyprint?
  1. #import <UIKit/UIKit.h> 
  2. typedef enum { 
  3. DirectionUnknown = 0, 
  4. DirectionLeft, 
  5. DirectionRight 
  6. } Direction; 
  7. @interface HappyGestureRecognizer : UIGestureRecognizer 
  8. @property (assign) int tickleCount; 
  9. @property (assign) CGPoint curTickleStart; 
  10. @property (assign) Direction lastDirection; 
  11. @end 
  12. #import <UIKit/UIKit.h>
  13. typedef enum {
  14. DirectionUnknown = 0,
  15. DirectionLeft,
  16. DirectionRight
  17. } Direction;
  18. @interface HappyGestureRecognizer : UIGestureRecognizer
  19. @property (assign) int tickleCount;
  20. @property (assign) CGPoint curTickleStart;
  21. @property (assign) Direction lastDirection;
  22. @end
復制代碼
.m文件 

[cpp] view plaincopyprint?
  1. #import "HappyGestureRecognizer.h" 
  2. #import <UIKit/UIGestureRecognizerSubclass.h> 
  3. #define REQUIRED_TICKLES 2 
  4. #define MOVE_AMT_PER_TICKLE 25 
  5. @implementation HappyGestureRecognizer 
  6. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
  7. UITouch * touch = [touches anyObject]; 
  8. self.curTickleStart = [touch locationInView:self.view]; 
  9. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
  10. // Make sure we've moved a minimum amount since curTickleStart 
  11. UITouch * touch = [touches anyObject]; 
  12. CGPoint ticklePoint = [touch locationInView:self.view]; 
  13. CGFloat moveAmt = ticklePoint.x - self.curTickleStart.x; 
  14. Direction curDirection; 
  15. if (moveAmt < 0) { 
  16. curDirection = DirectionLeft; 
  17. } else { 
  18. curDirection = DirectionRight; 
  19. if (ABS(moveAmt) < MOVE_AMT_PER_TICKLE) return; 
  20. // 確認方向改變了 
  21. if (self.lastDirection == DirectionUnknown || 
  22. (self.lastDirection == DirectionLeft && curDirection == DirectionRight) || 
  23. (self.lastDirection == DirectionRight && curDirection == DirectionLeft)) { 
  24. // 撓癢次數 
  25. self.tickleCount++; 
  26. self.curTickleStart = ticklePoint; 
  27. self.lastDirection = curDirection; 
  28. // 一旦撓癢次數超過指定數,設置手勢為結束狀態 
  29. // 這樣回調函數會被調用。 
  30. if (self.state == UIGestureRecognizerStatePossible && self.tickleCount > REQUIRED_TICKLES) { 
  31. [self setState:UIGestureRecognizerStateEnded]; 
  32. - (void)reset { 
  33. self.tickleCount = 0; 
  34. self.curTickleStart = CGPointZero; 
  35. self.lastDirection = DirectionUnknown; 
  36. if (self.state == UIGestureRecognizerStatePossible) { 
  37. [self setState:UIGestureRecognizerStateFailed]; 
  38. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
  39. [self reset]; 
  40. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
  41. [self reset]; 
  42. @end 
  43. #import "HappyGestureRecognizer.h"
  44. #import <UIKit/UIGestureRecognizerSubclass.h>
  45. #define REQUIRED_TICKLES 2
  46. #define MOVE_AMT_PER_TICKLE 25
  47. @implementation HappyGestureRecognizer
  48. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  49. UITouch * touch = [touches anyObject];
  50. self.curTickleStart = [touch locationInView:self.view];
  51. }
  52. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  53. // Make sure we've moved a minimum amount since curTickleStart
  54. UITouch * touch = [touches anyObject];
  55. CGPoint ticklePoint = [touch locationInView:self.view];
  56. CGFloat moveAmt = ticklePoint.x - self.curTickleStart.x;
  57. Direction curDirection;
  58. if (moveAmt < 0) {
  59. curDirection = DirectionLeft;
  60. } else {
  61. curDirection = DirectionRight;
  62. }
  63. if (ABS(moveAmt) < MOVE_AMT_PER_TICKLE) return;
  64. // 確認方向改變了
  65. if (self.lastDirection == DirectionUnknown ||
  66. (self.lastDirection == DirectionLeft && curDirection == DirectionRight) ||
  67. (self.lastDirection == DirectionRight && curDirection == DirectionLeft)) {
  68. // 撓癢次數
  69. self.tickleCount++;
  70. self.curTickleStart = ticklePoint;
  71. self.lastDirection = curDirection;
  72. // 一旦撓癢次數超過指定數,設置手勢為結束狀態
  73. // 這樣回調函數會被調用。
  74. if (self.state == UIGestureRecognizerStatePossible && self.tickleCount > REQUIRED_TICKLES) {
  75. [self setState:UIGestureRecognizerStateEnded];
  76. }
  77. }
  78. }
  79. - (void)reset {
  80. self.tickleCount = 0;
  81. self.curTickleStart = CGPointZero;
  82. self.lastDirection = DirectionUnknown;
  83. if (self.state == UIGestureRecognizerStatePossible) {
  84. [self setState:UIGestureRecognizerStateFailed];
  85. }
  86. }
  87. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  88. {
  89. [self reset];
  90. }
  91. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
  92. {
  93. [self reset];
  94. }
  95. @end
復制代碼

調用自定義手勢和上面一樣,回到這樣寫: 

[cpp] view plaincopyprint?
  1. - (void)handleHappy:(HappyGestureRecognizer *)recognizer{ 
  2. [self.hehePlayer play]; 
  3. - (void)handleHappy:(HappyGestureRecognizer *)recognizer{
  4. [self.hehePlayer play];
  5. }
復制代碼

手勢成功后播放呵呵笑的聲音。

在真機上運行,按住某個view,快速左右拖動,就會發出笑的聲音了。 

代碼解析:

先獲取起始坐標:curTickleStart

通過和ticklePoint的x值對比,得出當前的放下是向左還是向右。再算出移動的x的值是否比MOVE_AMT_PER_TICKLE距離大,如果太則返回。
再判斷是否有三次是不同方向的動作,如果是則手勢結束,回調。


參考:http://www.raywenderlich.com/656 ... nches-pans-and-more


例子代碼:http://download.csdn.net/detail/totogo2010/5094059

原文鏈接:http://blog.csdn.net/totogo2010/article/details/8615940
<ignore_js_op>

1361954419_1036.png (54.69 KB, 下載次數: 65)

 

1361954419_1036.png

<ignore_js_op>

1361953288_8283.png (139.31 KB, 下載次數: 77)

 

1361953288_8283.png

<ignore_js_op>

1361953008_9643.png (86.63 KB, 下載次數: 82)

 

1361953008_9643.png

原文:http://bbs.9ria.com/thread-197251-1-1.html
相關:http://blog.csdn.net/likendsl/article/details/7554150

文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()