2009-07-27 16 views
5

Estoy intentando arrastrar un UIImageView alrededor de la pantalla del iphone en mi aplicación.iPhone SDK: Problemas para arrastrar un UIImageView

Actualmente La funcionalidad de arrastre que he configurado está bien y al arrastrar la imagen la mueve por la pantalla, el problema es que no tiene que arrastrar la vista de imagen para moverla, también puede arrastrar a cualquier parte del pantalla y moverá la imagen. Soy nuevo en esta plataforma, así que no puedo pensar qué hacer para resolver este problema. Mi código actual para arrastrar la imagen es:

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { 
    CGPoint pt = [[touches anyObject] locationInView:pig]; 
    startLocation = pt; 


} 
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { 
    CGPoint pt = [[touches anyObject] locationInView:pig]; 
    CGRect frame = [pig frame]; 
    frame.origin.x += pt.x - startLocation.x; 
    frame.origin.y += pt.y - startLocation.y; 
    [pig setFrame: frame]; 
} 

Cualquier ayuda apreciada. Por cierto, cerdo es el UIImageView. También he notado que si configuro la interacción del usuario de la Vista de imagen "pig" para habilitarla, la imagen ya no se puede arrastrar, pero cuando no está configurada como habilitada, lo es.

Respuesta

0

Usted puede tratar de una subclase UIImageView e implementar touchesBegan: y touchesMoved: en él directamente. Luego, en InterfaceBuilder, seleccione su subclase para UIImageView.

alt text http://img.skitch.com/20090728-xetretcfubtcj7ujh1yc8165wj.jpg

Oh sí, ahora tendrá que establecer la interacción del usuario nuevo ...

+0

Muchas gracias, ¡eso funcionó a la perfección! – Sam

+0

self.userInteractionEnabled = YES; ... Siempre es lo mismo, y siempre es lo que estoy haciendo mal! Thanx epatel, genial :-) –

0

necesitará englobar en comenzar/commit secuencias de animación, al menos en su subproceso de temporizador:

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.02] /* 0.02 seconds suration*/ 

/* move pig coordinates here */ 

[UIView commitAnimations]; 

Guardar el nuevo movimiento en touchesMoved y luego procesarlos en el hilo temporizador.

0

Se puede utilizar una categoría de UIView para hacer que se pueda arrastrar y definir el área que pueden arrastrarse: https://github.com/rtoshiro/TXDragAndDrop

+0

Tenga en cuenta que [las respuestas solo de enlace] (http://meta.stackoverflow.com/tags/link-only-answers/info) no se recomiendan, entonces las respuestas deberían ser el punto final de un buscar una solución (frente a otra escala más de referencias, que tienden a quedar obsoletas en el tiempo). Considere agregar una sinopsis independiente aquí, manteniendo el enlace como referencia – kleopatra

Cuestiones relacionadas