1、UIGestureRecognizer介绍

手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加了移动设备使用便捷性。

iOS系统在3.2以后,为方便开发这使用一些常用的手势,提供了UIGestureRecognizer类。手势识别UIGestureRecognizer类是个抽象类,下面的子类是具体的手势,开发这可以直接使用这些手势识别。

UITapGestureRecognizer

UIPinchGestureRecognizer

UIRotationGestureRecognizer

UISwipeGestureRecognizer

UIPanGestureRecognizer

UILongPressGestureRecognizer

上面的手势对应的操作是:

  • Tap(点一下)
  • Pinch(二指往內或往外拨动,平时经常用到的缩放)
  • Rotation(旋转)
  • Swipe(滑动,快速移动)
  • Pan (拖移,慢速移动)
  • LongPress(长按)

UIGestureRecognizer的继承关系如下:

<ignore_js_op>

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. {
  3. CGPoint translation = [recognizer translationInView:self.view];
  4. recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
  5. recognizer.view.center.y + translation.y);
  6. [recognizer setTranslation:CGPointZero inView:self.view];
  7. }
  8. - (void) handlePan:(UIPanGestureRecognizer*) recognizer
  9. {
  10. CGPoint translation = [recognizer translationInView:self.view];
  11. recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
  12. recognizer.view.center.y + translation.y);
  13. [recognizer setTranslation:CGPointZero inView:self.view];
  14. }

复制代码

  • 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. {
  3. recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
  4. recognizer.scale = 1;
  5. }
  6. - (void) handlePinch:(UIPinchGestureRecognizer*) recognizer
  7. {
  8. recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
  9. recognizer.scale = 1;
  10. }

复制代码

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. {
  3. recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
  4. recognizer.rotation = 0;
  5. }
  6. - (void) handleRotate:(UIRotationGestureRecognizer*) recognizer
  7. {
  8. recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
  9. recognizer.rotation = 0;
  10. }

复制代码

添加了这几个手势后,运行看效果,程序中的imageView放了一个

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

的图片,在模拟器上拖动是没问题的。缩放和旋转有点问题,估计是因为在模拟器上的模拟的两个接触点距离在imageView的边界外了,所以操作无效果。

建议在真机上运行这个手势。

在模拟器上缩放和选择的操作技巧:

可以把imageView的frame值设置大一点,按住alt键,按下触摸板(不按下不行),这样就可以旋转和缩放了。

6、添加第二个ImagView并添加手势

记住:一个手势只能添加到一个View,两个View当然要有两个手势的实例了

[cpp] view plaincopyprint?

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

复制代码

多添加了一条龙的view,两个view都能接收上面的三种手势。运行效果如下:

<ignore_js_op>

7、拖动(pan手势)速度(以较快的速度拖放后view有滑行的效果)

如何实现呢?

  • 监视手势是否结束
  • 监视触摸的速度

[cpp] view plaincopyprint?

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

复制代码

代码实现解析:

  • 计算速度向量的长度(估计大部分都忘了)这些知识了。
  • 如果速度向量小于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. {
  3. return YES;
  4. }
  5. -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
  6. {
  7. return YES;
  8. }

复制代码

把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. }
  10. return player;
  11. }
  12. - (AVAudioPlayer *)loadWav:(NSString *)filename {
  13. NSURL * url = [[NSBundle mainBundle] URLForResource:filename withExtension:@"wav"];
  14. NSError * error;
  15. AVAudioPlayer * player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
  16. if (!player) {
  17. NSLog(@"Error loading %@: %@", url, error.localizedDescription);
  18. } else {
  19. [player prepareToPlay];
  20. }
  21. return player;
  22. }

复制代码

我会在最后例子代码给出完整代码,添加手势的步骤和前面一样的。

[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. }
  4. - (void)handleTap:(UITapGestureRecognizer *)recognizer {
  5. [self.chompPlayer play];
  6. }

复制代码

运行,点一下某个图,就会播放一个咬东西的声音。

不过这个点击播放声音有点缺陷,就是在慢慢拖动的时候也会播放。这使得两个手势重合了。怎么解决呢?使用手势的: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. }
  10. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  11. // Make sure we've moved a minimum amount since curTickleStart
  12. UITouch * touch = [touches anyObject];
  13. CGPoint ticklePoint = [touch locationInView:self.view];
  14. CGFloat moveAmt = ticklePoint.x - self.curTickleStart.x;
  15. Direction curDirection;
  16. if (moveAmt < 0) {
  17. curDirection = DirectionLeft;
  18. } else {
  19. curDirection = DirectionRight;
  20. }
  21. if (ABS(moveAmt) < MOVE_AMT_PER_TICKLE) return;
  22. // 确认方向改变了
  23. if (self.lastDirection == DirectionUnknown ||
  24. (self.lastDirection == DirectionLeft && curDirection == DirectionRight) ||
  25. (self.lastDirection == DirectionRight && curDirection == DirectionLeft)) {
  26. // 挠痒次数
  27. self.tickleCount++;
  28. self.curTickleStart = ticklePoint;
  29. self.lastDirection = curDirection;
  30. // 一旦挠痒次数超过指定数,设置手势为结束状态
  31. // 这样回调函数会被调用。
  32. if (self.state == UIGestureRecognizerStatePossible && self.tickleCount > REQUIRED_TICKLES) {
  33. [self setState:UIGestureRecognizerStateEnded];
  34. }
  35. }
  36. }
  37. - (void)reset {
  38. self.tickleCount = 0;
  39. self.curTickleStart = CGPointZero;
  40. self.lastDirection = DirectionUnknown;
  41. if (self.state == UIGestureRecognizerStatePossible) {
  42. [self setState:UIGestureRecognizerStateFailed];
  43. }
  44. }
  45. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  46. {
  47. [self reset];
  48. }
  49. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
  50. {
  51. [self reset];
  52. }
  53. @end
  54. #import "HappyGestureRecognizer.h"
  55. #import <UIKit/UIGestureRecognizerSubclass.h>
  56. #define REQUIRED_TICKLES 2
  57. #define MOVE_AMT_PER_TICKLE 25
  58. @implementation HappyGestureRecognizer
  59. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  60. UITouch * touch = [touches anyObject];
  61. self.curTickleStart = [touch locationInView:self.view];
  62. }
  63. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  64. // Make sure we've moved a minimum amount since curTickleStart
  65. UITouch * touch = [touches anyObject];
  66. CGPoint ticklePoint = [touch locationInView:self.view];
  67. CGFloat moveAmt = ticklePoint.x - self.curTickleStart.x;
  68. Direction curDirection;
  69. if (moveAmt < 0) {
  70. curDirection = DirectionLeft;
  71. } else {
  72. curDirection = DirectionRight;
  73. }
  74. if (ABS(moveAmt) < MOVE_AMT_PER_TICKLE) return;
  75. // 确认方向改变了
  76. if (self.lastDirection == DirectionUnknown ||
  77. (self.lastDirection == DirectionLeft && curDirection == DirectionRight) ||
  78. (self.lastDirection == DirectionRight && curDirection == DirectionLeft)) {
  79. // 挠痒次数
  80. self.tickleCount++;
  81. self.curTickleStart = ticklePoint;
  82. self.lastDirection = curDirection;
  83. // 一旦挠痒次数超过指定数,设置手势为结束状态
  84. // 这样回调函数会被调用。
  85. if (self.state == UIGestureRecognizerStatePossible && self.tickleCount > REQUIRED_TICKLES) {
  86. [self setState:UIGestureRecognizerStateEnded];
  87. }
  88. }
  89. }
  90. - (void)reset {
  91. self.tickleCount = 0;
  92. self.curTickleStart = CGPointZero;
  93. self.lastDirection = DirectionUnknown;
  94. if (self.state == UIGestureRecognizerStatePossible) {
  95. [self setState:UIGestureRecognizerStateFailed];
  96. }
  97. }
  98. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  99. {
  100. [self reset];
  101. }
  102. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
  103. {
  104. [self reset];
  105. }
  106. @end

复制代码

调用自定义手势和上面一样,回到这样写:

[cpp] view plaincopyprint?

  1. - (void)handleHappy:(HappyGestureRecognizer *)recognizer{
  2. [self.hehePlayer play];
  3. }
  4. - (void)handleHappy:(HappyGestureRecognizer *)recognizer{
  5. [self.hehePlayer play];
  6. }

复制代码

手势成功后播放呵呵笑的声音。

在真机上运行,按住某个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)

<ignore_js_op>

1361953288_8283.png (139.31 KB, 下载次数: 77)

<ignore_js_op>

1361953008_9643.png (86.63 KB, 下载次数: 82)

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

UIGestureRecognizer ios手势识别温习的更多相关文章

  1. 【转】iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势) -- 不错不错

    原文网址:http://blog.csdn.net/totogo2010/article/details/8615940 1.UIGestureRecognizer介绍 手势识别在iOS上非常重要,手 ...

  2. iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)

    iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)       1.UIGestureRecognizer介绍 手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加 ...

  3. ios iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)

    iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势) 转自容芳志大神的博客:http://www.cnblogs.com/stoic/archive/2013/02/27/2940 ...

  4. 介绍一些实用的IOS手势识别库 (COCOS2D)

    http://www.supersuraccoon-cocos2d.com/zh/2012/11/14/introduction-to-some-great-ios-gesture-recogniti ...

  5. ios手势识别代理

    之前做优质派时写了个仿网易新闻导航的第三方,由于当时做项目时这个主控制器就是RootViewController,虽然用的是ScrollView但也没考虑到导航栏的手势返回的问题 ,现在做小区宝3.0 ...

  6. iOS手势识别

    一.手势识别与触摸事件 1.如果想监听一个view上面的触摸事件,可选的做法是: (1)自定义一个view (2)实现view的touches方法,在方法内部实现具体处理代码 2.通过touches方 ...

  7. iOS,手势识别简单使用

    1.iOS目前支持的手势识别(6种) 2.点按手势和慢速拖动手势简单使用 iOS目前支持的手势识别(6种) UITapGestureRecognizer(点按) UIPinchGestureRecog ...

  8. iOS 手势识别

    首先给大家解释一下为什么要学习手势识别? 如果想监听一个UIView上面的触摸事件,之前的做法是: 自定义一个UIView : 实现UIView的touches方法,在方法里面实现具体功能 透过tou ...

  9. IOS手势识别,捏合,旋转,轻扫等

    ref:http://blog.csdn.net/rechard_chen/article/details/51769972   //点按手势的创建,这里需要实现响应事件的方法 UITapGestur ...

随机推荐

  1. [CareerCup] 9.11 Parenthesize the Expression 表达式加括号

    9.11 Given a boolean expression consisting of the symbols 0,1, &, |, and ^, and a desired boolea ...

  2. mysqldumpslow使用说明

    mysqldumpslow使用说明 mysqldumpslow --help Usage: mysqldumpslow [ OPTS... ] [ LOGS... ] Parse and summar ...

  3. java并发:中断一个正在运行的线程

    要使任务和线程能安全可靠地停止,并不是一件很容易的事情,java没有提供任何机制来安全地终止线程,那么我们该怎么办呢? 下面我们先来了解一下java中的中断机制: java中断机制是一种协作机制,也就 ...

  4. 01.C#数据类型、排序、过滤(一章1.1-1.2)

    随着看的文章及探讨越多,越发现自己实在太不定性了,看下<C#深入理解>吧,做一下读书笔记,从中发现自己的不足.闲话不说,进入正题吧. 在C#1中定下一个简单的数据类型 public cla ...

  5. Linq之Expression初见

    目录 写在前面 系列文章 Expression 表达式树创建方式 一个例子 总结 写在前面 上篇文章介绍了扩展方法,这篇文章开始将陆续介绍在linq中使用最多的表达式树的相关概念,以概念及例子一一列出 ...

  6. WCF入门 (14)

    前言 上周去面试,跪了,这一年没什么长进,还是挺惭愧的. 得到的评语是:想的太多,做的太少. 做了一份面试题,最后一题是数据库的,写个查询.要查出Score有两次及两次以上超过79的Name和他的最高 ...

  7. JS面向对象概述

    这部分内容还是比较难理解的,像借用构造函数这种方法,实际工作中还是很常见的,不过对于后面的寄生理解还有点困难,只能慢慢学习了. 思维导图

  8. SDAutolayout图片大小根据数量变化

    只需要在自定义的PhotoContainerView中做一下判断就可以了 ) { [self setupAutoWidthFlowItems:[temp copy] withPerRowItemsCo ...

  9. MySQL 索引背后的数据结构及算法原理

    本文转载自http://blog.jobbole.com/24006/ 摘要本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引 ...

  10. redmine慢---提速方法

    最近readmine速度越来越慢.网上查了一下,把其自带的服务器webrick 换成为mongrel,可以解决这个问题,具体操作方法如下,方法: gem install mongrel 如果提示类似“ ...