¿Es posible arrastrar UIView por la pantalla de iOS mientras tiene imágenes y texto? p.ej. tarjetas pequeñas ¿Podría señalarme el tema similar (resuelto)? No he encontrado ninguno.UIView arrastre (imagen y texto)
Respuesta
Mientras que UIView no tiene un soporte integrado para moverse a lo largo del usuario arrastrando, no debería ser tan difícil implementarlo. Es aún más fácil cuando solo se trata de arrastrar en la vista, y no de otras acciones como tocar, tocar dos veces, tocar varias veces, etc.
Lo primero que debe hacer es crear una vista personalizada, digamos DraggableView
, por subclassing UIView. Luego, anule el método touchesMoved:withEvent:
de UIView, y puede obtener una ubicación de arrastre actual allí, y mover el DraggableView. mira el siguiente ejemplo.
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *aTouch = [touches anyObject];
CGPoint location = [aTouch locationInView:self.superview];
[UIView beginAnimations:@"Dragging A DraggableView" context:nil];
self.frame = CGRectMake(location.x, location.y,
self.frame.size.width, self.frame.size.height);
[UIView commitAnimations];
}
Y porque todas las subvistas del objeto DraggableView se moverán también. Así que ponga todas sus imágenes y textos como subvistas del objeto DraggableView.
Lo que implementé aquí es muy simple. Sin embargo, si desea comportamientos más complejos para el arrastre (por ejemplo, el usuario tiene que tocar la vista durante unos segundos para mover la vista), tendrá que anular otros métodos de manejo de eventos (touchesBegan: withEvent: y touchesEnd: withEvent) también.
Además de MHC's answer.
Si no desea que la esquina superior izquierda de la vista para saltar bajo el dedo, también puede anular touchesBegan
así:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *aTouch = [touches anyObject];
offset = [aTouch locationInView: self];
}
y cambiar touchesMoved de MHC a:
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *aTouch = [touches anyObject];
CGPoint location = [aTouch locationInView:self.superview];
[UIView beginAnimations:@"Dragging A DraggableView" context:nil];
self.frame = CGRectMake(location.x-offset.x, location.y-offset.y,
self.frame.size.width, self.frame.size.height);
[UIView commitAnimations];
}
también debe definir CGPoint offset
en la interfaz:
@interface DraggableView : UIView
{
CGPoint offset;
}
EDIT:
Arie Litovsky proporciona una solución más elegante que le permite deshacerse de la Ivar: https://stackoverflow.com/a/10378382/653513
A pesar de que la solución rokjarc funciona, usando
CGPoint previousLocation = [aTouch previousLocationInView:self.superview];
evita la CGPoint offset
creación y el llame al touchesBegan:withEvent:
Esto es una buena solución, basada en pepouze ' s respuesta, se vería como (probado, ¡funciona!)
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *aTouch = [touches anyObject];
CGPoint location = [aTouch locationInView:self];
CGPoint previousLocation = [aTouch previousLocationInView:self];
self.frame = CGRectOffset(self.frame, (location.x - previousLocation.x), (location.y - previousLocation.y));
}
Este código hace que UIVIew ** not ** salte bajo su dedo, bien hecho. –
Aquí es una solución para arrastrar a custom UIView (que se puede escalar o rotar a través de su transform
), que puede contener imágenes y/o texto (sólo editar el Tile.xib
según sea necesario):
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
CGPoint previous = [touch previousLocationInView:self];
if (!CGAffineTransformIsIdentity(self.transform)) {
location = CGPointApplyAffineTransform(location, self.transform);
previous = CGPointApplyAffineTransform(previous, self.transform);
}
self.frame = CGRectOffset(self.frame,
(location.x - previous.x),
(location.y - previous.y));
}
Esto funciona para mí.Mi UIView girar y escalar
- (void) touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
CGPoint previous = [touch previousLocationInView:self];
if (!CGAffineTransformIsIdentity(self.transform)) {
location = CGPointApplyAffineTransform(location, self.transform);
previous = CGPointApplyAffineTransform(previous, self.transform);
}
CGRect newFrame = CGRectOffset(self.frame,
(location.x - previous.x),
(location.y - previous.y));
float x = CGRectGetMidX(newFrame);
float y = CGRectGetMidY(newFrame);
self.center = CGPointMake(x, y);
}
- 1. Toque y arrastre la imagen en android
- 2. Zoom y arrastre una imagen en android
- 3. IOS arrastre, suelte o arroje un UIView
- 4. Imagen de arrastre al tocar
- 5. convertir uiview a .png imagen
- 6. iOS UIView imagen de fondo
- 7. JTable - arrastre y suelte
- 8. jquery arrastre y gire en el ángulo
- 9. UII detección de contacto táctil y arrastre
- 10. UIView shadow y InterfaceBuilder
- 11. WPF arrastre y suelte en DataGrid
- 12. NSTableView y arrastre y suelte desde Finder
- 13. Cacao: Cómo transformar una imagen de arrastre mientras arrastra
- 14. Combinar imagen y texto para dibujar
- 15. Botón con imagen y texto centrado
- 16. Botón con texto Y una imagen (Android)
- 17. Hiperenlace HTML con imagen y texto
- 18. UIView y AutoresizingMask ignoraron
- 19. personalizada UIView y becomeFirstResponder
- 20. UIView y subvistas - opacidad
- 21. Android 2.2 Haga clic y arrastre la imagen centrada en contacto
- 22. PyQT4: Arrastre y suelte archivos en QListWidget
- 23. d3 clic y evento de arrastre anidación
- 24. Cómo crear una imagen desde un UIView/UIScrollView
- 25. ¿Cómo mostrar una imagen en UIView mediante programación?
- 26. Superposición de UIView con una imagen de fondo
- 27. android arrastre view smooth
- 28. html5 - arrastre un lienzo
- 29. arrastre a otro proceso
- 30. clon node en arrastre
uno de los buenos. Pero debes usarlo como lo sugiere diablosnuevos, con 'CGRectOffset'. –
Agradable, no requiere punto de contacto –