2011-03-30 13 views
5

estoy triyng para hacer un reconocedor de gestos para un simple UIView:¿No se ha agregado UITapGestureRecognizer a un UIView?

UIView *theView = [[UIView alloc] initWithFrame:rect]; 
[theView setUserInteractionEnabled:YES]; 

UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc] initWithTarget:self 
                     action:@selector(handleTap)] autorelease]; 
[theView addGestureRecognizer:tap]; 

Si puedo depurar los gestureRecognizers propiedad de la vista que muestra el objeto gesto reconocedor. Pero cuando toco dentro de la vista, no funciona.

El mismo código usando un UIImageView funciona perfecto, ¿alguna idea de por qué no funciona en UIView?

ACTUALIZADO:

Una clase de ejemplo.

@implementation ExampleClass 

- (UIView *)getViewInRect:(CGRect)rect 
{ 
    UIView *theView = [[UIView alloc] initWithRect:rect]; 
    [theView setUserInteractionEnabled:YES]; 

    UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc] 
            initWithTarget:self 
            action:@selector(handleTap)] 
            autorelease]; 
    [aText addGestureRecognizer:tap]; 

    return theView; 
} 

- (UIImageView *)getImageViewInRect:(CGRect)rect 
{ 
    UIImageView *theView = [[UIImageView alloc] initWithRect:rect]; 
    [theView setUserInteractionEnabled:YES]; 

    UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc] 
             initWithTarget:self 
               action:@selector(handleTap)] 
            autorelease]; 
    [theView addGestureRecognizer:tap]; 

    return [theView autorelease];  
} 

- (void)handleTap 
{ 
    NSLog(@"Tap Handled!!", nil); 
} 

@end 

Actualización 2:

Adición UITapGestureRecognizer a todos subvistas de theview no se soluciona el problema ...

FIX IT !!!

OK !! ¡El problema era el CGRect para theView, tenía el ancho establecido en 0.0!

+0

¿Cómo es su función handleTap? ¿Está utilizando un gestureRecognizer en la Superview de theView, que podría captar el gesto? – Seega

+0

handleTap es un método de la misma clase donde vive el código anterior. Desde esta primicia puedo llamarlo sin problemas con [self handleTap]. –

+1

¿Está diciendo que si reemplazó 'UIView' con' UIImageview' en el fragmento de código anterior, funciona? – freespace

Respuesta

2

Declara la vista como ivar en archivo .h. Sintetizar y luego llamarlo así:

[self.theview setMultipleTouchEnabled:YES]; 

No se olvide de alloc e init que theview en el método viewDidLoad.

Eso es todo.

+1

No solucione el problema, lo siento :-( –

19

¿Has probado esto?

[view setUserInteractionEnabled:YES]; 
Cuestiones relacionadas