11

Estoy usando UITapGestureRecognizer porque estoy usando un UIScrollView que actúa como un contenedor para mi UILabel s. Básicamente, estoy tratando de usar un método de acción con argumentos para poder, por ejemplo, envíe myLabel.tag valor al método de acción para saber qué acción tomar dependiendo de qué UILabel haya sido activado por un toque.UITapGestureRecognizer initWithTarget: action: ¿método para tomar argumentos?

Una forma de hacerlo es tener tantos métodos de acción como UILabel s, pero eso no es muy "bonito" en código. Lo que me gustaría lograr es solo tener un método de acción con declaraciones de cambio.

Es esto posible o voy a tener que hacerlo de esta manera (suspiro):

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

UITapGestureRecognizer *myLabel2Tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myLabel2Tap)]; 
[myLabel1Tap addGestureRecognizer:myLabel2Tap]; 

UITapGestureRecognizer *myLabelNTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myLabelNTap)]; 
[myLabel1Tap addGestureRecognizer:myLabelNTap]; 

- (void)myLabel1Tap { 
// Perform action 
} 

- (void)myLabel2Tap { 
// Perform action 
} 

- (void)myLabelNTap { 
// Perform action 
} 

Respuesta

17

añadir un solo gesto reconocedor de la opinión de que es la supervista de sus diversas etiquetas:

UITapGestureRecognizer *myLabelTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myLabelTapHandler:)]; 
[myLabelParent addGestureRecognizer:myLabelTap]; 

Entonces cuando manipule el gesto, determinar qué etiqueta fue aprovechado:

-(void)myLabelTapHandler:(UIGestureRecognizer *)gestureRecognizer { 
    UIView *tappedView = [gestureRecognizer.view hitTest:[gestureRecognizer locationInView:gestureRecognizer.view] withEvent:nil]; 

    // do something with it 
} 
+0

no tienen Olvidaste la semicolumna en el selector? – alecnash

5

Usted puede utilizar un solo UITapGestureRecognizer y en el controlador gesto (su myLaberXTap), que tiene la sintaxis:

- (void)handleGesture:(UITapGestureRecognizer*)gestureRecognizer { 
    ... 
} 

usa gesture.view para saber en qué vista estás trabajando.

Cuestiones relacionadas