2010-12-20 23 views
14

Estoy tratando de mover una UIView en relación con los toques del usuario.Mover UIView con relación al tacto

Esto es lo que tengo en este momento:

int oldX, oldY; 
BOOL dragging; 

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

    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint touchLocation = [touch locationInView:self.view]; 

    if (CGRectContainsPoint(window.frame, touchLocation)) { 
     dragging = YES; 
     oldX = touchLocation.x; 
     oldY = touchLocation.y; 
    } 

} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint touchLocation = [touch locationInView:self.view]; 

    if (CGRectContainsPoint(window.frame, touchLocation) && dragging) { 
     CGRect frame; 
     frame.origin.x = (window.frame.origin.x + touchLocation.x - oldX); 
     frame.origin.y = (window.frame.origin.y + touchLocation.y - oldY); 
     window.frame = frame; 

    } 

} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    dragging = NO; 
} 

La vista mantiene el parpadeo de un lugar a otro, y no sé qué más hacer.

Cualquier ayuda apreciada.

Respuesta

12

Modificar la touchesBegan y touchesMoved métodos para ser como la siguiente.

float oldX, oldY; 
BOOL dragging; 

El touchesBegan: método: withEvent.

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

    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint touchLocation = [touch locationInView:self.view]; 

    if (CGRectContainsPoint(window.frame, touchLocation)) { 

     dragging = YES; 
     oldX = touchLocation.x; 
     oldY = touchLocation.y; 
    } 
} 

El touchesMoved: método: withEvent.

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

    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint touchLocation = [touch locationInView:self.view]; 

    if (dragging) { 

     CGRect frame = window.frame; 
     frame.origin.x = window.frame.origin.x + touchLocation.x - oldX; 
     frame.origin.y = window.frame.origin.y + touchLocation.y - oldY; 
     window.frame = frame; 
    } 
} 

El touchesEnded: método: withEvent.

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

    dragging = NO; 
} 
+0

¿ventana es el objeto que está arrastrando? – EmptyStack

+0

Sí, es una vista creada en Interface Builder. –

+0

Es mucho mejor, pero extrañamente se aleja cuando se toca. –

62

Lo que quiere es usar un UIPanGestureRecognizer, introducido en iOS 3.2. Se utiliza con algo tan sencillo como este (de su UIViewController subclase):

-(void)viewDidLoad; 
{ 
    [super viewDidLoad]; 
    UIPanGestureRecognizer* pgr = [[UIPanGestureRecognizer alloc] 
             initWithTarget:self 
               action:@selector(handlePan:)]; 
    [self.panningView addGestureRecognizer:pgr]; 
    [pgr release]; 
} 

-(void)handlePan:(UIPanGestureRecognizer*)pgr; 
{ 
    if (pgr.state == UIGestureRecognizerStateChanged) { 
     CGPoint center = pgr.view.center; 
     CGPoint translation = [pgr translationInView:pgr.view]; 
     center = CGPointMake(center.x + translation.x, 
          center.y + translation.y); 
     pgr.view.center = center; 
     [pgr setTranslation:CGPointZero inView:pgr.view]; 
    } 
} 
+1

Desplazamiento mucho más suave !!! – user523234

+3

¡Esta respuesta es mejor que la respuesta aceptada! – Rick

+1

Si ve una vista de UIImage, asegúrese de que la interacción del usuario habilitada para SÍ como: _imageView.userInteractionEnabled = YES; – ddiego

0

Aquí es código en Swift, una versión ligeramente más generalista que trabaja con un ImageView que se ha creado en la pantalla.

let panGestureRecongnizer = UIPanGestureRecognizer(target:self, action: "handlepan:") 
imageView.addGestureRecognizer(panGestureRecongnizer) 

func handlepan(sender: UIPanGestureRecognizer) { 
    if (sender.state == UIGestureRecognizerState.Changed) { 
     var center = sender.view?.center 
     let translation = sender.translationInView(sender.view) 
     center = CGPointMake(center!.x + translation.x, center!.y + translation.y) 
     sender.view?.center = center! 
     sender .setTranslation(CGPointZero, inView: sender.view) 
    } 
} 
Cuestiones relacionadas