2011-06-15 18 views
26

Estoy intentando adjuntar el reconocedor de gestos a mi propia clase, que es la subclase de UILabel, pero no funciona. ¿Puede ayudarme a entender lo que está mal en el códigoEs posible adjuntar UITapGestureRecognizer a la subclase UILabel


@interface Card : UILabel { 

} 

- (void) addBackSideWord; 

@end 

#import "Card.h" 

@implementation Card 
- (id)initWithFrame:(CGRect)frame { 

    if ((self = [super initWithFrame:frame])) { 

     UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] 
         initWithTarget:self action:@selector(addBackSideWord)]; 
     [tapRecognizer setNumberOfTouchesRequired:2]; 
     [tapRecognizer setDelegate:self]; 
     [self addGestureRecognizer:tapRecognizer]; 
    } 

    return self; 
} 

- (void) addBackSideWord { 

    //do something 
} 
@end 

Respuesta

68

El código debería funcionar bien, la única cosa que puede que tenga que corregir es que la interacción del usuario está deshabilitada para UILabel por defecto, así reconocedor gesto no recibe ningún tocar eventos. Intente activarlo manualmente mediante la adición de esta línea a su código (por ejemplo, en el método init):

self.userInteractionEnabled = YES; 
+1

Gracias por sus respuestas! Pasé 24 horas leyendo documentación y no noté este simple truco. Solo espero que me haya sido útil :) – Michael

15

Sí, eso es posible, Cualquier clase heredó de UIView.

No olvide activar la interacción del usuario.

self.userInteractionEnabled = YES; 
+1

¡Gracias por tus respuestas! Pasé 24 horas leyendo documentación y no noté este simple truco. Solo espero que me haya sido útil :) – Michael

+0

@Michael: En algún momento ... :) – Jhaliya

2

Puede utilizar a continuación código para agregar gesto golpecito en UILable: -

Paso 1:

Delegate "UIGestureRecognizerDelegate" to your viewcontroller.h 

for example: 
    @interface User_mail_List : UIViewController<UIGestureRecognizerDelegate> 

Paso 2:

//create you UILable 
UILabel *title_lbl= [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)]; 
[title_lbl setText:@"u&me"]; 
[title_lbl setUserInteractionEnabled:YES]; 
[yourView addSubview:title_lbl]; 

Paso 3:

UITapGestureRecognizer *tap= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Prof_lbl_Pressed:)];//your action selector 
[tap setNumberOfTapsRequired:1]; 
title_lbl.userInteractionEnabled= YES; 
[title_lbl addGestureRecognizer:tap]; 

Paso 4:

-(void)Prof_lbl_Pressed:(id)sender{ 
    //write your code action 
} 

gracias,

Cuestiones relacionadas