2012-04-24 25 views
7

Ho un UIGestureRecognizer che voglio lavorare su due diverse Uiview, entrambe nella stessa gerarchia di vista di un UiViewController. L'azione di UIGestureRecognizer è circa la stessa su ciascuna, quindi vorrei che fosse richiamata la stessa funzione (lo sarà, ovviamente) e dirò a runtime quale delle UIViews con cui ho a che fare. Ma come? Non riesco a vedere che UIGestureRecognizer trasporta le informazioni dell'oggetto con esso. Mi manca la riga nella documentazione o il gestureRecognizer non sa a quale oggetto è stato allegato che è stato chiamato? Sembra che il punto della lingua sarebbe che lo saprebbe.UIGestureRecognizer sa quale oggetto è chiamato?

In alternativa, forse sto equivoco l'intento della classe e che non dovrei:

UITapGestureRecognizer *dblTap = 
[[UITapGestureRecognizer alloc] initWithTarget: self 
             action: @selector(handleDblTap:)]; 
[viewA addGestureRecognizer: dblTap]; 
[viewB addGestureRecognizer: dblTap]; 

e poi si aspettano di essere in grado di:

-(void)handleDblTap: (UIGestureRecognizer *)gestureRecognizer 
{ 
    if (viewA)... 

Se infatti UIGestureRecognizer non supporta essendo collegato a più oggetti contemporaneamente, quindi, se sai perché non supporta questo, potresti educarmi? Grazie per l'aiuto.

+1

@NJones è giusto. Una vista per riconoscimento. Puoi anche impostare un tag per ciascun riconoscimento per scoprire quale riconoscitore ha chiamato quel metodo. –

risposta

21

Lo standard è una vista per riconoscimento. Ma puoi ancora utilizzare in modo efficiente un metodo di gestione.

Si potrebbe istanziare i riconoscitori in questo modo:

UITapGestureRecognizer *dblTapViewA = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDblTap:)]; 
[viewA addGestureRecognizer: dblTapViewA]; 

UITapGestureRecognizer *dblTapViewB = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDblTap:)]; 
[viewB addGestureRecognizer: dblTapViewB]; 

Allora il vostro metodo del gestore potrebbe somigliare:

-(void)handleDblTap:(UITapGestureRecognizer *)tapRec{ 
    if (tapRec.view == viewA){ 
     // double tap view a 
    } else if (tapRec.view == viewB) { 
     // double tap view b 
    } 
} 
+0

Questo è stato ben spiegato per me. Grazie. – StoneBreaker

-1
UITapGestureRecognizer *dblTapA = 
[[UITapGestureRecognizer alloc] initWithTarget: self 
            action: @selector(handleDblTap:)]; 
[viewA addGestureRecognizer: dblTapA]; 

UITapGestureRecognizer *dblTapB = 
[[UITapGestureRecognizer alloc] initWithTarget: self 
            action: @selector(handleDblTap:)]; 
[viewA addGestureRecognizer: dblTapB]; 
0

È possibile assegnare un tag per visualizzare e poi semplicemente confrontare che taggare ed eseguire azioni.

UITapGestureRecognizer *dblTap = 
[[UITapGestureRecognizer alloc] initWithTarget: self 
            action: @selector(handleDblTap:)]; 
[view addGestureRecognizer: dblTap]; 
view.tag = 2000; // set any integer 

E durante la chiamata

-(void)handleDblTap:(UITapGestureRecognizer *)tapRec{ 
    if (tapRec.view.tag == 2000){ 
     // double tap view with tag 
    } 
} 
Problemi correlati