2011-07-05 15 views
8

¿Cómo se puede detectar si se tocó una imagen en Xcode? He mirado en la documentación de Apple y es muy confuso ... vi:Cómo detectar si se toca la imagen

if (CGRectContainsPoint([imageView frame], location)) 

pero mi imagen fija no se moverá. He intentado utilizar touchesBegan + touchesMoved, y la haga userInteractionIsEnabled de la imagen en sí, pero todavía no lo detectará :(


EDITAR: Gracias todos por sus sugerencias Al final, Tenía muchas ganas de que sea lo más simple posible, y sabía que mi código debe trabajo, así que mantuvo jugando con él, y después de dormir bien por la noche, me di cuenta de que era una solución bastante sencilla:

En mis toquesMoved:

UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint location = [touch locationInView:touch.view]; 

    CGRect shapeRect = [imageView frame]; 
    CGRect dropSquareRect = [dropSquare frame]; 

    CGPoint touchLocation = CGPointMake(location.x, location.y); 

    if (CGRectContainsPoint(shapeRect, touchLocation)) 
    { 
     [UIView beginAnimations:nil context:nil]; 
     [UIView setAnimationDuration:.3]; 
     [imageView setCenter:touchLocation]; 
     [UIView commitAnimations]; 
    } 

    if (CGRectIntersectsRect(shapeRect, dropSquareRect)) 
    { 
     [UIView beginAnimations:nil context:nil]; 
     [UIView setAnimationDuration:.3]; 
     [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
     self.imageView.alpha = 0; 
     self.imageView.center = CGPointMake(dropSquare.center.x, dropSquare.center.y); 
     self.imageView.transform = CGAffineTransformMakeScale(0.8, 0.8); 
     [UIView commitAnimations]; 

Respuesta

3

Puede agregar un UIPanGesureRecognizer al UIImageView que contiene el UIImage. Esto le permitirá detectar cuando el usuario está haciendo un paneo en la imagen y mover la traducción de las imágenes en consecuencia. La referencia a la documentación está aquí.

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPanGestureRecognizer_Class/Reference/Reference.html

Es bueno el uso de los reconocedores gesto, ya que mantiene la coherencia con el resto del sistema operativo en lo que va paneo.

Espero que esto ayude.

+0

Esto es lo que debe hacerse ... dios respuesta –

3

Necesita el método touchesBegan p. Ej.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
CGPoint myPoint = [touch locationInView:self]; 
    if (CGRectContainsPoint([imageView frame], location)){ 
    // code here    
    } 
} 

Acabo de leer que lo intentó, aunque no estoy seguro de por qué no funciona. Intente utilizar el gesto de tocar como se sugiere a continuación.

2

Se puede utilizar esta:

hacer después de ajuste a la vista se ha cargado:

UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)]; 
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight; 
[rightRecognizer setNumberOfTouchesRequired:1]; 
[mainSlideShowImageScrollView addGestureRecognizer:rightRecognizer]; 
[rightRecognizer release]; 
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeHandle:)]; 
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft; 
[leftRecognizer setNumberOfTouchesRequired:1]; 
[mainSlideShowImageScrollView addGestureRecognizer:leftRecognizer]; 
[leftRecognizer release]; 

A continuación, utilice los métodos siguientes:

- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer 
    { 
     //Do moving 
    } 

- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer 
    { 
     // do moving 
    } 
+0

respuesta muy irrelevante ... –

13

usted podría considerar el uso de UITapGestureRecognizer con UIImageView para detectar los toques.

Y también no se olvide de configurar userInteractionIsEnabled a YES.

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self    action:@selector(imageTapped:)]; 
myImageView.userInteractionIsEnabled = YES; 
[myImageView addGestureRecognizer:tap]; 
[tap release]; 

Implemento imageTapped: method.

- (void)imageTapped:(UITapGestureRecognizer *) gestureRecognizer 
    { 

    } 
3

Puede probar este Suponga que tiene un

IBOutlet UIImageView *touchImageVIew; 
Let touchImageVIew height and width are 50,25; 

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { 
    // Retrieve the touch point 

    CGPoint pt = [[touches anyObject] locationInView:touchImageVIew]; 

    if (pt.x>=0 && pt.x<=50 && pt.y>=0 && pt.y<=25) { 
     NSLog(@"image touched"); 
    } 

    else 
{ 
NSLog(@"image not touched"); 
} 
} 

ajuste de altura, la anchura y el nombre de acuerdo a sus necesidades.

+1

Esta es una gran solución. – Siriss

2

Si no es tan particular en el uso de UIImageView, intente usar un botón personalizado con su imagen y utilice los métodos de acción, toqueDown y toqueDraggedOut para mover la imagen.

Cuestiones relacionadas