2012-02-28 24 views
6

Obviamente no entendí UIImageView vs UIImage, para registrar gesto (como tocar/clic) en la imagen, necesito tener una imagen en uiimageview y registrar gesto en uiimageview no en uiimage.cómo hacer que se haga clic en mi imagen

Así que este fragmento de código funciona para mí:

- (void)drawRect:(CGRect)rect {     
    Obj *obj = ... obj has imageHref which is NSString 

    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:obj.imageHref]]; 
    [image drawInRect:CGRectMake((self.frame.size.width/2) - (image.size.with/2), (self.frame.size.height/2) - (image.size.height/2), image.size.width, image.size.height)]; 

} 

Esto me da la imagen centrada, pero inútil porque quiero que se pueda hacer clic, así que aunque sólo lo hago con UIImageView lugar como esto:

- (void)drawRect:(CGRect)rect {     
         Obj *obj = ... obj has imageHref which is NSString 

         UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:obj.imageHref]]; 

    CGRect rect1=CGRectMake((self.frame.size.width/2) - (image.size.width/2), (self.frame.size.height/2) - (image.size.height/2), image.size.width, image.size.height); 

    UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:radioStationImage]; 

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)]; 

    [backgroundImageView addGestureRecognizer:tap]; 

         [backgroundImageView drawRect:rect1]; 

     } 

Solo quiero hacer clic en esta imagen para hacer algún tipo de acción al hacer clic, ¿qué tan difícil puede ser esto?

EDIT:

En realidad estoy de aplicación:

https://github.com/andreyvit/SoloComponents-iOS/tree/master/ATPagingView

de desplazamiento que ofrece la página ordenada. Pero con imágenes se puede hacer clic en el centro de la pantalla, no la página 1, 2, etc ..

EDIT 2:

Cualquier perspectiva sobre esto hoy?

Respuesta

2

Por defecto UIImageView tiene su propiedad userInteractionEnabled en NO.

hacer:

[imageView setUserInteractionEnabled:YES]; 
+0

He actualizado pregunta quizá que pueden ayudarle a donde Estoy preguntando desde – London

7

que no es necesario dibujo personalizado (drawRect:) para esto. Simplemente agregue UIImageView como subvista al cargar la vista (en Interface Builder o en loadView dependiendo de cómo esté inicializando su vista). Luego puede agregar UITapGestureRecognizer a esa vista mientras lo hace.

Como notas de @jackslash, también debe activar la interacción del usuario para la vista de la imagen una vez que haya hecho eso.


Creo que estás haciendo esto mucho más complejo de lo que es. No es necesario dibujar a medida con drawRect:. No es necesario crear subclases UIImageView (las referencias de link @ madmik3 no son relevantes para iOS4 +). Probablemente hay necesidad de un UIView subclase real (se puede hacer todo esto dentro del controlador de vista), pero aquí es como lo haría en una vista subclase:

- (id)initWithFrame:(CGRect)frame { 
    self = [super initWithFrame:frame]; 
    if (self) { 
    Obj *obj = ... obj has imageHref which is NSString 
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:obj.imageHref]]; 
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((self.frame.size.width/2) - (image.size.with/2), (self.frame.size.height/2) - (image.size.height/2), image.size.width, image.size.height)]; 
    imageView.userInteractionEnabled = YES; 
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)]; 
    [imageView addGestureRecognizer:tap]; 

    [self addSubview:imageView]; 
    } 
    return self; 
} 
+0

He actualizado la pregunta tal vez que puede ayudarle cuando estoy preguntando desde – London

+0

que también anula el UIImageView como lo que es http://stackoverflow.com/questions/1848040/detect-touch-on-uiview-with -interface-builder/4494304 # 4494304 – madmik3

+1

@ madmik3 es todo esto necesario solo para centrar la imagen y hacer que se pueda hacer clic (guau). y era optimista acerca de aprender iOS ... – London

Cuestiones relacionadas