【发布时间】:2012-11-24 20:37:59
【问题描述】:
我有一个按钮,我正在测试它的点击,点击它改变背景颜色,点击两下另一种颜色,再点击三下另一种颜色。 代码是:
- (IBAction) button
{
UITapGestureRecognizer *tapOnce = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnce:)];
UITapGestureRecognizer *tapTwice = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTwice:)];
UITapGestureRecognizer *tapTrice = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTrice:)];
tapOnce.numberOfTapsRequired = 1;
tapTwice.numberOfTapsRequired = 2;
tapTrice.numberOfTapsRequired = 3;
//stops tapOnce from overriding tapTwice
[tapOnce requireGestureRecognizerToFail:tapTwice];
[tapTwice requireGestureRecognizerToFail:tapTrice];
//then need to add the gesture recogniser to a view - this will be the view that recognises the gesture
[self.view addGestureRecognizer:tapOnce];
[self.view addGestureRecognizer:tapTwice];
[self.view addGestureRecognizer:tapTrice];
}
- (void)tapOnce:(UIGestureRecognizer *)gesture
{
self.view.backgroundColor = [UIColor redColor];
}
- (void)tapTwice:(UIGestureRecognizer *)gesture
{
self.view.backgroundColor = [UIColor blackColor];
}
- (void)tapTrice:(UIGestureRecognizer *)gesture
{
self.view.backgroundColor = [UIColor yellowColor];
}
问题是第一个水龙头不起作用,另一个是。 如果我在没有按钮的情况下使用此代码,它会完美运行。 谢谢。
【问题讨论】:
-
您是否在点击按钮时添加此手势?为什么不将它添加到 viewDidLoad 中?
-
因为我必须只在一小部分视图上使用这个手势。
-
但是你的代码在整体上设置手势
self.view。那么你应该按照我的答案进行更改。
标签: objective-c ios xcode button gesture