2011-05-03 18 views
6

Simplemente estoy tratando de agarrar el UIImageView que fue aprovechado, desde el UIScrollView.Detectar un solo toque en UIScrollView que contiene 'n' UIImageViews

He encontrado 2 formas de lograr lo anterior en la Web.

1er método: cree un gesto de tap en uiimageview antes de agregarlo a scrollviewer.

Este método no me ha funcionado. el método handleSingleTap nunca se llama.

No tengo ideas sobre lo que estoy haciendo mal/por qué esto no funciona.

UITapGestureRecognizer *singleTap = 
    [[UITapGestureRecognizer alloc] initWithTarget:self 
    action:@selector(handleSingleTap:)]; 
     singleTap.numberOfTapsRequired = 1; 
     [imageView addGestureRecognizer:singleTap]; 
     [singleTap release]; 

     [framesSourceScrollview addSubview:imageView]; 
     [imageView release]; 

      - (void)handleSingleTap:(UIGestureRecognizer *)sender 
      { 
       NSLog(@"image tapped!!!"); 
      } 

segunda Método: UIScrollView subclase

@interface SingleTapScrollViewer : UIScrollView { 
} 
@end 

@implementation SingleTapScrollViewer 

- (id)initWithFrame:(CGRect)frame 
{ 
    return [super initWithFrame:frame]; 
} 

- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event 
{ 
    // If not dragging, send event to next responder 
    if (!self.dragging) 
     [self.nextResponder touchesEnded: touches withEvent:event]; 
    else 
     [super touchesEnded: touches withEvent: event]; 
} 
@end 

In the ViewController 

- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event 
{ 
    // Process the single tap here 
    NSLog(@"Scroll view single tapped. touches count : %d", touches.count); 
UITouch *touch = [touches anyObject]; 
    UIImageView *imgView = (UIImageView*)touch.view; 
    NSLog(@"tag is %@", imgView.tag); 
} 

Usando este método, el respondedor touchesDown se consiga llamar pero los toques '[anyobject]' no es el UIImageView que se dio un golpecito en.

Intenté establecer la 'etiqueta' en cada UIImageView que estoy agregando a la vista de desplazamiento con un contador en aumento, pero me devuelvo 0 sin importar en qué imagen toque.

Soy nuevo en cocoa en general y no estoy seguro de cómo aprovechar esta respuesta de otra manera.

Cualquier sugerencia/sugerencia sería genial.

gracias de antemano.

Respuesta

12

configuró el userInteractionEnabled = YES en el UIImageView. Está desactivado por defecto.

+0

Whoa !!. Jason, muchas gracias. No tenía ni idea de esto. ¡¡¡Eso ayuda!!!. Estaba creando 'n' uiimageviewers en el código. ¿Es este estándar en todos los controles de cacao? –

+0

no, normalmente encendido. Solo algunas de las vistas que normalmente no tienen interacción se han desactivado. UILabel, UIImageView son los dos que vienen a la mente. –

+1

tendría sentido que si alguien agregaba un UIGestureRecognizer a un UIView, que el userInterationEnabled cambiara automáticamente a ON ... pero de nuevo, nadie de Apple alguna vez me preguntara mi opinión. :-) –

Cuestiones relacionadas