2011-03-19 16 views
13

Estoy tratando de usar un único reconocedor de tap en un imageView (que también es un niño scrollView). En Interface Builder, he creado y referido el scrollView solamente. Funciona el desplazamiento, pero el evento de un solo toque no se reconoce (no se registra nada). Aquí está el código:UITapGestureRecognizer en la subvista UIScrollView

- (void)loadView { 
[super loadView]; 

UIImage *myImage = [UIImage imageNamed:@"img1.jpg"]; 
imageView = [[UIImageView alloc] initWithImage:myImage]; 

[myImage release]; 

// add gesture recognizers to the image view 
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]; 

[imageView addGestureRecognizer:singleTap]; 

[singleTap release]; 

[imageScrollView setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)]; 

[imageScrollView addSubview:imageView]; } 

- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer { 
// single tap handling 
NSLog(@"sinlgeTap called"); } 

He mirado por horas y probado muchas cosas. Tal vez la mirada de otra persona puede ayudar mucho más. Gracias.

+1

habilitando la interacción del usuario para imageView, disparará singleTap ... pero el desplazamiento ya no funcionará. – brainondev

+2

¿Hay alguna manera de tener la funcionalidad de desplazamiento y un reconocedor de gestos trabajando juntos? –

Respuesta

31

después de haber habilitado el userInteraction para imageView y también la opción canCancelContentTouches para la ScrollView:

[imageScrollView setCanCancelContentTouches:YES]; 
[imageView setUserInteractionEnabled:YES]; 

Funciona ahora.

+1

¿por qué tienes que [imageScrollView setCanCancelContentTouches: YES]? También en el caso de que tenga una imagen, ¿qué ocurre si hay varias imágenes? ¿Qué código tiene en handleSingleTap para averiguar en qué imagen ocurrió el evento tap – Yogesh

+1

? El UIGestureRecognizer tiene view como una propiedad. Así que puedes hacer algo como UIImageView * thisView = UIGestureRecognizer.view, probablemente también puedas usar UIImageView, no estoy seguro si eso sería necesario UIImageView * thisView = (UIImageView *) UIGestureRecognizer.view – PruitIgoe

+0

Sí por @Yogesh, solo necesitas realmente para usar [imageView setUserInteractionEnabled: YES] para que esto funcione. – JimmyJammed

Cuestiones relacionadas