Tôi có một nút và tôi đang thử nghiệm các vòi trên đó, với một lần nhấn, nó thay đổi màu nền, với hai lần nhấn một màu khác và ba lần nhấn một lần nữa. Mã này là:iOS - Nhấn đúp vào uibutton
- (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]; }
Vấn đề là lần nhấn đầu tiên không hoạt động, tùy chọn còn lại có. Nếu tôi sử dụng mã này mà không có nút, nó hoạt động hoàn hảo. Cảm ơn.
Bạn có thêm cử chỉ này vào nút nhấn không? tại sao bạn không thêm nó trong viewDidLoad? – iDev
Vì tôi chỉ sử dụng cử chỉ này trên một phần nhỏ. – Kerberos
Nhưng mã của bạn đang thiết lập các cử chỉ trên toàn bộ 'self.view'. Bạn nên thay đổi nó như được hiển thị trong câu trả lời của tôi sau đó. – iDev