手势之间是互斥的,如果你想同时触发蛇和龙的view,那么需要实现协议
UIGestureRecognizerDelegate,
- @interface ViewController : UIViewController<UIGestureRecognizerDelegate>
- @end
并在协议这个方法里返回YES。
- -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
- {
- return YES;
- }
把self作为代理设置给手势:
- panGestureRecognizer.delegate = self;
- pinchGestureRecognizer.delegate = self;
- rotateRecognizer.delegate = self;
这样可以同时拖动或旋转缩放两个view了。
9、tap点击手势
这里为了方便看到tap的效果,当点击一下屏幕时,播放一个声音。
为了播放声音,我们加入AVFoundation.framework这个框架。
- - (AVAudioPlayer *)loadWav:(NSString *)filename {
- NSURL * url = [[NSBundle mainBundle] URLForResource:filename withExtension:@"wav"];
- NSError * error;
- AVAudioPlayer * player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
- if (!player) {
- NSLog(@"Error loading %@: %@", url, error.localizedDescription);
- } else {
- [player prepareToPlay];
- }
- return player;
- }
我会在最后例子代码给出完整代码,添加手势的步骤和前面一样的。
- #import <UIKit/UIKit.h>
- #import <AVFoundation/AVFoundation.h>
-
- @interface ViewController : UIViewController<UIGestureRecognizerDelegate>
- @property (strong) AVAudioPlayer * chompPlayer;
- @property (strong) AVAudioPlayer * hehePlayer;
-
- @end
- - (void)handleTap:(UITapGestureRecognizer *)recognizer {
- [self.chompPlayer play];
- }
运行,点一下某个图,就会播放一个咬东西的声音。
不过这个点击播放声音有点缺陷,就是在慢慢拖动的时候也会播放。这使得两个手势重合了。怎么解决呢?使用手势的:requireGestureRecognizerToFail方法。
10、手势的依赖性
在viewDidLoad的循环里添加这段代码:
- [tapRecognizer requireGestureRecognizerToFail:panGestureRecognizer];
意思就是,当如果pan手势失败,就是没发生拖动,才会出发tap手势。这样如果你有轻微的拖动,那就是pan手势发生了。tap的声音就不会发出来了。
11、自定义手势
自定义手势继承:UIGestureRecognizer,实现下面的方法:
- – touchesBegan:withEvent:
- – touchesMoved:withEvent:
- – touchesEnded:withEvent:
- - touchesCancelled:withEvent:
新建一个类,继承UIGestureRecognizer,代码如下:
.h文件
- #import <UIKit/UIKit.h>
- typedef enum {
- DirectionUnknown = 0,
- DirectionLeft,
- DirectionRight
- } Direction;
-
- @interface HappyGestureRecognizer : UIGestureRecognizer
- @property (assign) int tickleCount;
- @property (assign) CGPoint curTickleStart;
- @property (assign) Direction lastDirection;
-
- @end
.m文件
- #import "HappyGestureRecognizer.h"
- #import <UIKit/UIGestureRecognizerSubclass.h>
- #define REQUIRED_TICKLES 2
- #define MOVE_AMT_PER_TICKLE 25
-
- @implementation HappyGestureRecognizer
-
- - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
- UITouch * touch = [touches anyObject];
- self.curTickleStart = [touch locationInView:self.view];
- }
-
- - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
-
-
- UITouch * touch = [touches anyObject];
- CGPoint ticklePoint = [touch locationInView:self.view];
- CGFloat moveAmt = ticklePoint.x - self.curTickleStart.x;
- Direction curDirection;
- if (moveAmt < 0) {
- curDirection = DirectionLeft;
- } else {
- curDirection = DirectionRight;
- }
- if (ABS(moveAmt) < MOVE_AMT_PER_TICKLE) return;
-
-
- if (self.lastDirection == DirectionUnknown ||
- (self.lastDirection == DirectionLeft && curDirection == DirectionRight) ||
- (self.lastDirection == DirectionRight && curDirection == DirectionLeft)) {
-
-
- self.tickleCount++;
- self.curTickleStart = ticklePoint;
- self.lastDirection = curDirection;
-
-
-
- if (self.state == UIGestureRecognizerStatePossible && self.tickleCount > REQUIRED_TICKLES) {
- [self setState:UIGestureRecognizerStateEnded];
- }
- }
-
- }
-
- - (void)reset {
- self.tickleCount = 0;
- self.curTickleStart = CGPointZero;
- self.lastDirection = DirectionUnknown;
- if (self.state == UIGestureRecognizerStatePossible) {
- [self setState:UIGestureRecognizerStateFailed];
- }
- }
-
- - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
- {
- [self reset];
- }
-
- - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
- {
- [self reset];
- }
-
- @end
调用自定义手势和上面一样,回到这样写:
- - (void)handleHappy:(HappyGestureRecognizer *)recognizer{
- [self.hehePlayer play];
- }
手势成功后播放呵呵笑的声音。
在真机上运行,按住某个view,快速左右拖动,就会发出笑的声音了。