移动UIView与触摸的关系

我试图移动UIView与用户触摸的关系。

这是我目前所拥有的:

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

视图从一个位置闪烁到另一个位置,我不知道还有什么要做。

任何帮助赞赏。

touchesBegantouchesMoved方法修改为如下所示。

 float oldX, oldY; BOOL dragging; 

touchesBegan: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; } } 

touchesMoved: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; } } 

touchesEnded:withEvent:方法。

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

你想要的是使用iOS 3.2中引入的UIPanGestureRecognizer 。 你可以像这样使用它(从你的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]; } } 

这里是Swift中的代码,它是一个稍微更通用的版本,与在屏幕上创build的ImageView一起工作。

 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) } }