iOS版 - 上的UIButton枪王 [英] iOS - Double tap on uibutton

查看:231
本文介绍了iOS版 - 上的UIButton枪王的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,我测试了水龙头就可以了,有一个水龙头它改变背景颜色,有两个水龙头另一种颜色,并具有3个抽头另一种颜色一次。
在code是:

I have a button and I'm testing the taps on it, with one tap it change a background color, with two taps another color and with three taps another color again. The code is:

- (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]; }

的问题是,第一抽头不工​​作,其他的是。
如果我用这个code,无按钮,它完美的作品。
谢谢你。

The problem is that the first tap don't works, the other yes. If I use this code without button it works perfectly. Thanks.

推荐答案

如果您想要的颜色改变按钮的水龙头,你应该在 viewDidLoad中法左右,而不是在同一个按钮动作。以上code将反复上的按钮的点击添加手势 self.view ,而不是按钮

If you want the colors to change on tap of button, you should add these gestures on button in viewDidLoad method or so rather than on the same button action. The above code will repeatedly add gestures on tap of the button to the self.view and not on button.

- (void)viewDidLoad {
      [super viewDidLoad];
      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.button addGestureRecognizer:tapOnce]; //remove the other button action which calls method `button`
      [self.button addGestureRecognizer:tapTwice];
      [self.button addGestureRecognizer:tapTrice];
}

这篇关于iOS版 - 上的UIButton枪王的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆