2012-12-06 12 views
14

Ho un pulsante e sto testando i tocchi su di esso, con un tocco cambia colore di sfondo, con due tocchi di un altro colore e con tre tocchi di nuovo un altro colore. Il codice è:iOS - Tocca due volte sul pulsante uu

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

Il problema è che il primo rubinetto Non opere, l'altro sì. Se uso questo codice senza pulsante funziona perfettamente. Grazie.

+0

Stai aggiungendo questi gesti al tocco dei pulsanti? perché non lo aggiungi in viewDidLoad? – iDev

+0

Perché devo usare questo gesto solo su una piccola parte della vista. – Kerberos

+0

Ma il tuo codice sta impostando i gesti sull'intero 'self.view'. Dovresti cambiarlo come mostrato nella mia risposta. – iDev

risposta

17

Se si desidera che i colori cambino toccando il pulsante, è necessario aggiungere questi pulsanti sul pulsante nel metodo viewDidLoad piuttosto che sulla stessa azione del pulsante. Il codice precedente aggiungerà ripetutamente dei gesti alla pressione del pulsante su self.view e non su 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]; 
} 
Problemi correlati