iphone开发中的手势操作:Multiple Taps

用Automatic Gesture Recognition实现: 

在viewDidLoad中

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UITapGestureRecognizer *singleTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tap1)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:singleTap];

UITapGestureRecognizer *doubleTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tap2)];
doubleTap.numberOfTapsRequired = 2;
doubleTap.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:doubleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];

UITapGestureRecognizer *tripleTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tap3)];
tripleTap.numberOfTapsRequired = 3;
tripleTap.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:tripleTap];
[doubleTap requireGestureRecognizerToFail:tripleTap];

UITapGestureRecognizer *quadrupleTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tap4)];
quadrupleTap.numberOfTapsRequired = 4;
quadrupleTap.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:quadrupleTap];
[tripleTap requireGestureRecognizerToFail:quadrupleTap];
}

添加自定义响应方法:

- (void)tap1 {
singleLabel.text = @"Single Tap Detected";
[self performSelector:@selector(eraseMe:)
withObject:singleLabel afterDelay:1.6f];
}

- (void)tap2 {
doubleLabel.text = @"Double Tap Detected";
[self performSelector:@selector(eraseMe:)
withObject:doubleLabel afterDelay:1.6f];
}

- (void)tap3 {
tripleLabel.text = @"Triple Tap Detected";
[self performSelector:@selector(eraseMe:)
withObject:tripleLabel afterDelay:1.6f];
}

- (void)tap4 {
quadrupleLabel.text = @"Quadruple Tap Detected";
[self performSelector:@selector(eraseMe:)
withObject:quadrupleLabel afterDelay:1.6f];
}

- (void)eraseMe:(UILabel *)label {
label.text = @"";
}

其实也可以直接在touchesBegan中自己实现功能:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
NSUInteger tapCout = [touch tapCount];

switch (tapCout) {
case 1:
[self performSelector:@selector(tap1) withObject:nil afterDelay:.4];//延迟执行
break;
case 2:
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(tap1) object:nil];//取消tap1
[self performSelector:@selector(tap2) withObject:nil afterDelay:.4];
break;
case 3:
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(tap2) object:nil];//取消tap2
[self performSelector:@selector(tap3) withObject:nil afterDelay:.4];
break;
case 4:
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(tap3) object:nil];//取消tap3
[self tap4];
break;
default:
break;
}
}



that‘s all.

posted on 2012-03-26 10:43  老Zhan  阅读(684)  评论(0编辑  收藏  举报