2012-12-06 34 views
14

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.

+0

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

+0

Vì tôi chỉ sử dụng cử chỉ này trên một phần nhỏ. – Kerberos

+0

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

Trả lời

17

Nếu bạn muốn thay đổi màu trên nút nhấn, bạn nên thêm các cử chỉ này vào nút trong phương thức viewDidLoad hoặc hơn là trên cùng một thao tác nút. Mã trên sẽ liên tục thêm cử chỉ trên vòi của nút vào số self.view chứ không phải trên 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]; 
} 
Các vấn đề liên quan