Mover UIView con relación al tacto

Estoy intentando mover una UIView con relación a los toques del usuario.

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 sigue parpadeando de un lugar a otro, y no sé qué más hacer.

Cualquier ayuda apreciada.

Modifique los toques de inicio y toque los methods de movimiento para que sean como los siguientes.

 float oldX, oldY; BOOL dragging; 

The touchesBegan: withEvent: method.

 - (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; } } 

Los toques mudados: withEvent: método.

 - (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; } } 

The touchesEnded: withEvent: method.

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

Lo que quiere es usar un UIPanGestureRecognizer , presentado en iOS 3.2. Lo usas con algo tan fácil como este (desde tu subclass UIViewController ):

 -(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]; } } 

Aquí hay un código en Swift, una versión ligeramente más generalist que funciona con una 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) } }