Pregunta

Estoy tratando de arrastrar un UIImageView alrededor de la pantalla del iPhone en mi aplicación.

Tiene la función de arrastrar he levantado está bien y arrastrando la imagen se mueve por la pantalla, el problema es que usted no tiene que arrastrar la vista de imagen para moverla, también puede arrastrar en cualquier lugar de la pantalla y se moverá la imagen. Soy nuevo en esta plataforma, así que no puedo pensar en 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 me he dado cuenta de que si me puse la interacción con el usuario de la imagen de Visión "cerdo" para habilitar la imagen ya no es capaz de arrastrar, pero cuando no se establece en Enabled Es.

¿Fue útil?

Solución

Se puede tratar de una subclase UIImageView e implementar touchesBegan: y touchesMoved: en él directamente. Luego, en InterfaceBuilder seleccionar la subclase para la UIImageView.

  

texto alternativo http://img.skitch.com/20090728-xetretcfubtcj7ujh1yc8165wj.jpg

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

Otros consejos

Es necesario rodearla de 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.

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top