触动开始,触动已结束,触动移动UIView移动

我需要拖动我的UIView对象。 我使用这个代码,但它不工作

float oldX, oldY; BOOL dragging; - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint touchLocation = [touch locationInView:self]; if ([[touch.view class] isSubclassOfClass:[UILabel class]]) { UILabel *label = (UILabel *)touch.view; if (CGRectContainsPoint(label.frame, touchLocation)) { dragging = YES; oldX = touchLocation.x; oldY = touchLocation.y; } } } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { dragging = NO; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint touchLocation = [touch locationInView:self]; if ([[touch.view class] isSubclassOfClass:[UILabel class]]) { UILabel *label = (UILabel *)touch.view; if (dragging) { CGRect frame = label.frame; frame.origin.x = label.frame.origin.x + touchLocation.x - oldX; frame.origin.y = label.frame.origin.y + touchLocation.y - oldY; label.frame = frame; } } } 

你的代码有点奇怪

你问self的位置,一些UIView大概包含你想检查的UILabel 。 这引出了一个问题,为什么你不把touchesXXX添加到你的某个UILabel子类中。

这取消了,因为你使用label.frame是根据它的superview.bounds(你的父母UIView你问触摸位置的方面)定义,但这并不是最直接的方法来跟随什么继续。

我会build议使用UIPanGestureRecognizer

 -(void)dragging:(UIPanGestureRecognizer *)gesture { // Check if this is the first touch if(gesture.state == UIGestureRecognizerStateBegan) { // Store the initial touch so when we change positions we do not snap self.panCoord = [gesture locationInView:gesture.view]; [self.view bringSubviewToFront:gesture.view]; } CGPoint newCoord = [gesture locationInView:gesture.view]; // Create the frame offsets to use our finger position in the view. float dX = newCoord.x-self.panCoord.x; float dY = newCoord.y-self.panCoord.y; gesture.view.frame = CGRectMake(gesture.view.frame.origin.x+dX, gesture.view.frame.origin.y+dY, gesture.view.frame.size.width, gesture.view.frame.size.height); } 

这只是我的一个偏好。 对我来说,使用手势识别器,然后使用touchesBegantouchesEndedtouchesMoved是一个简单得多。 如果UIPanGestureRecognizer我将使用这些。

这是移动UIView对象的工作代码

float oldX,oldY; BOOL拖动;

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint touchLocation = [touch locationInView:touch.view]; if ([[touch.view class] isSubclassOfClass:[UILabel class]]) { dragging = YES; oldX = touchLocation.x; oldY = touchLocation.y; } } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { dragging = NO; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint touchLocation = [touch locationInView:touch.view]; if ([[touch.view class] isSubclassOfClass:[UILabel class]]) { UILabel *label = (UILabel *)touch.view; if (dragging) { CGRect frame = label.frame; frame.origin.x = label.frame.origin.x + touchLocation.x - oldX; frame.origin.y = label.frame.origin.y + touchLocation.y - oldY; label.frame = frame; } } } 

有了这个代码,我可以移动我的UIView对象的任何地方。 我的ViewController.swift看起来像这样。

 // code from below import UIKit class ViewController: UIViewController { var location = CGPoint(x: 0, y: 0) @IBOutlet weak var Person: UIImageView! override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { var touch : UITouch! = touches.first! as UITouch location = touch.location(in: self.view) Person.center = location } override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { var touch : UITouch! = touches.first! as UITouch location = touch.location(in: self.view) Person.center = location } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. Person.center = CGPoint(x: 160, y: 330) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } //ends 

希望它有帮助,虽然这是另一种方式来做,而不是提到的问题。