iOS - Double tap on uibutton

前端 未结 1 696
孤街浪徒
孤街浪徒 2020-12-15 19:44

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:

相关标签:
1条回答
  • 2020-12-15 19:51

    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];
    }
    
    0 讨论(0)
提交回复
热议问题