2011-12-28 15 views
8

En mi iPhone App Quiero funcionalidad implementada como se muestra a continuacióniPhone App: aplicación de arrastrar y soltar imágenes en UIView

enter image description here

usuario puede escoger una forma (una imagen) de una vista y poner en otra vista

¿Cómo puedo lograr esto?

Por favor, ayuda y sugiere.

Gracias.

+0

[http://stackoverflow.com/questions/17678735/adding-drag-and-drop-component-to-ios-app/31963306#31963306](http://stackoverflow.com/questions/17678735/adding-drag-and-drop- component-to-ios-app/31963306 # 31963306) Puede probar este enlace enjoy coding :) – SARATH

Respuesta

4

Try a continuación enlace drag and drop image around the screen

también try this

+1

hola, tu primera lin k está muerto. para futuras referencias, puede publicar los contenidos del enlace en su comentario. SO está destinado a ser una ventanilla única y no debe vincular a enlaces potencialmente muertos – lizzy81

-2

Tres solución paso

1) ua tener que implementar/escuchar eventos táctiles

2) aplicar arrastrando y táctiles Eventos

3) y luego administrar método movetosuperview.

+0

Código fuente de cómo arrastrar y soltar con ajuste al área http://stackoverflow.com/a/9233102/1537178 –

5

La manera más fácil es utilizar un UIPanGestureRecognizer. Eso le dará mensajes cuando la vista se mueva y cuando se caiga. Cuando se mueve, actualiza su centro. Cuando se cae, compruebe si su posición está dentro de los límites de la vista que desea colocar. (Puede necesitar convertir coordenadas al sistema de coordenadas de la vista de destino.) Si lo es, realice la acción adecuada.

1

Se puede hacer referencia a la anterior entrada que sin duda ayudará a lograr esta funcionalidad ...

  1. Basic Drag and Drop in iOS
  2. Building drag and drop interface on iphone
  3. iPhone drag/drop

Para todos enlace anterior, Debe tener en cuenta que primero necesita obtener el evento TouchesBegin para cualquier control y luego debe obtener el TouchesMoved evento para el mismo control.

En el evento TouchesMoved, solo tiene que obtener el punto central (CGPoint) del Control. Y cuando suelte el control se establecerá en ese CGPoint. Si esto crea un problema, puede tomar esa variable CGPoint y establecer ese punto en el evento TouchesEnded.

Para su caso, creo que debe tener para mantener la jerarquía de las Vistas ... Else mientras arrastra ver pueden no ser visibles ...

PARA MÁS parte codificante:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

     NSLog(@"%f,%f", self.center.x, self.center.y); 
     CGPoint newLoc = CGPointZero; 

     newLoc = [self.mainView convertPoint:[[touches anyObject] locationInView:self.superview] toView:self.superview]; 
     float newX = newLoc.x + self.superview.frame.origin.x + (self.frame.size.width /2) + [[touches anyObject] locationInView:self].x ; 
     float newY = newLoc.y - (((UIScrollView *)self.superview).contentOffset.y *2) ; 
     NSLog(@"content offset %f", ((UIScrollView *)self.superview).contentOffset.y); 

     self.scrollParent.scrollEnabled = NO; 
     NSLog(@"%f,%f", self.center.x, self.center.y); 

     newLoc = CGPointMake(newX, newY); 
     [self.superview touchesCancelled:touches withEvent:event];                
     [self removeFromSuperview]; 
     NSLog(@"%f,%f", self.center.x, self.center.y); 


     self.center = CGPointMake(newLoc.x, newLoc.y); 
     [self.mainView addSubview:self]; 
     NSLog(@"%f,%f", self.center.x, self.center.y); 

     [self.mainView bringSubviewToFront:self]; 
     isInScrollview = NO; 
} 

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    [UIView beginAnimations:@"stalk" context:nil]; 
    [UIView setAnimationDuration:.001]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 

    UITouch *touch = [touches anyObject]; 

    self.center = [touch locationInView: self.superview]; 

    [UIView commitAnimations]; 
    if ((self.center.x + (self.frame.size.width/2)) > 150 && hasExitedDrawer && !self.scrollParent.dragging) { 
     self.scrollParent.scrollEnabled = NO; 

     [self.delegate moveItemsDownFromIndex: ((self.center.y + (self.scrollParent.contentOffset.y))/44) + 1 ]; 
     //NSLog(@"%i", ((self.center.y + (self.scrollParent.contentOffset.y *2))/44) + 1); 
    } 

    if (self.center.x + (self.frame.size.width/2) < 150) { 

     hasExitedDrawer = YES; 

    } 

} 

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

    if ((self.center.x + (self.frame.size.width/2)) > 150 && hasExitedDrawer && !self.scrollParent.dragging) { 
     CGPoint newLoc = CGPointZero; 

     newLoc = [self.scrollParent convertPoint:[[touches anyObject] locationInView:self.superview] toView:self.superview]; 
     float newY = newLoc.y + (self.scrollParent.contentOffset.y *2); 

     [self.scrollParent insertSubview:self atIndex:((self.center.y + (self.scrollParent.contentOffset.y))/44) ]; 
     self.frame = CGRectMake(0, newY, self.frame.size.width, self.frame.size.height); 
     isInScrollview = YES; 
     hasExitedDrawer = NO; 
    } 
} 

Este código puede parecer algo irrelevante, pero le da más idea ...