¿Cómo se hace que una imagen siga el dedo en el objetivo-c?¿Cómo se hace que una imagen siga a su dedo en Objective-C?
Respuesta
- Cree su UIView y configurarlo (o cualquier subclase de los mismos tales como UIImageView)
- Ajuste la posición de su imagen para estar donde el usuario está tocando:
Hay cuatro delegar métodos para aceptar eventos táctiles que son parte de cualquier clase que hereda de UIResponder, como UIView. Use el método delegado que sea más apropiado para usted. Si desea que siga el dedo, esto será -touchesMoved:
- (void) touchesMoved:(NSSet*)toucheswithEvent:(UIEvent*)event {
CGPoint pt = [[touches anyObject] locationInView:self];
myImageView.center = pt;
}
Otros métodos de delegado disponibles para usted son:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
me escribió un ejemplo de aplicación que hace exactamente lo que quiere. Es una demostración de dibujo de gráficos Quartz 2D, pero dibuja un cuadrado rojo y el círculo negro en el que arrastrar el dedo y debe ser lo suficientemente fácil de seguir:
alt text http://brockwoolf.com/shares/stackoverflow/3445494/screen.png
Download Xcode project (32kb)
La Manzana La biblioteca Sample Code viene con un ejemplo bien escrito, llamado Touches. También demuestra la nueva característica UIGestureRecognizers en iOS 4.0.
hay una publicación excelente aquí.
por Divan Visagie
Este es el código correspondiente (tomada desde el enlace de arriba):
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//Find the path for the menu resource and load it into the menu array
NSString *menuPlistPath = [[NSBundle mainBundle] pathForResource:@"Menu" ofType:@"plist"];
menuArray = [[NSArray alloc] initWithContentsOfFile:menuPlistPath];
//add some gestures
// UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeLeft:)];
// [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
//[self.view addGestureRecognizer:swipeLeft];
// UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight:)];
// [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
//[self.view addGestureRecognizer:swipeRight];
}
float difference;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint contentTouchPoint = [[touches anyObject] locationInView:content];
difference = contentTouchPoint.x;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint pointInView = [[touches anyObject] locationInView:self.view];
float xTarget = pointInView.x - difference;
if(xTarget > menuTable.frame.size.width)
xTarget = menuTable.frame.size.width;
else if(xTarget < 0)
xTarget = 0;
[UIView animateWithDuration:.25
animations:^{
[content setFrame:CGRectMake(xTarget, content.frame.origin.y, content.frame.size.width, content.frame.size.height)];
}
];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint endPoint = [[touches anyObject] locationInView:self.view];
float xTarget = endPoint.x - difference;
if(xTarget < (menuTable.frame.size.width/2))
xTarget = 0;
else
xTarget = menuTable.frame.size.width;
[UIView animateWithDuration:.25
animations:^{
[content setFrame:CGRectMake(xTarget, content.frame.origin.y, content.frame.size.width, content.frame.size.height)];
}
];
}
- 1. IOS: trace una línea con su dedo
- 2. Haz que una imagen siga el puntero del mouse
- 3. Android dibujando una línea para seguir su dedo
- 4. ¿Anclar href de una imagen hace que se descargue?
- 5. jquery demora que se siga un enlace
- 6. Cómo asegurarme de que una aplicación siga ejecutándose en Linux
- 7. ¿Cómo animarías algo para que siga una curva?
- 8. ¿Cómo hace NSValue su magia?
- 9. Cómo dejar que una imagen HTML se superponga a otra
- 10. Cómo evitar que apache http client siga una redirección
- 11. Cómo puedo hacer que una imagen gire en función de la dirección de deslizamiento de mi dedo
- 12. enviar una notificación desde javascript en UIWebView a ObjectiveC
- 13. ¿Cómo se hace que un clip div sea su contenido visible?
- 14. Arrastrar UIView debajo del dedo
- 15. ¿Cómo se hace una imagen para seguir el puntero del mouse con jquery?
- 16. IKImageView hace que la aplicación se cuelgue
- 17. ¿Hay alguna manera de obligar a WebView a actualizar su imagen (vista) mientras se desplaza?
- 18. Cómo eliminar una rama y todos los objetos a los que hace referencia
- 19. ¿Cómo hago que JScrollPane scroll siga el enfoque de entrada?
- 20. ¿Cómo guardar una imagen en su formato original?
- 21. ¿Cómo puedo hacer que UIImageView cambie su imagen mediante programación?
- 22. Recortar una imagen PNG a su tamaño mínimo
- 23. WPF: ¿Cómo mostrar una imagen en su tamaño original?
- 24. ¿Qué hace que un widget Qt y su diseño se comporten correctamente (en relación con su tamaño)?
- 25. ¿Cómo se hace una excepción para definir su propio mensaje en C#?
- 26. ¿Cómo formatearé Zend_Form_Element_Radio para que la etiqueta siga la entrada?
- 27. ¿Cómo haces que div siga mientras te desplazas?
- 28. ¿Cómo puedo hacer que System.Net.Http.HttpClient no siga 302 redirecciones?
- 29. Coloque una imagen fuera de su contenedor
- 30. Jquery si su elemento primera vez que se hace clic en
El enlace está inactivo, se puede actualizar por favor? Tengo exactamente el mismo problema ! – Seb