Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
651 views
in Technique[技术] by (71.8m points)

objective c - iOS - Double tap on uibutton

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...