最简单的代码,用于将UIImage视图从A点移动到B点

@IBOutlet weak var imageView: UIImageView! override func viewWillAppear(animated: Bool) { self.imageView.center.y -= view.bounds.height } override func viewDidAppear(animated: Bool) { UIView.animateWithDuration(0.5, delay: 0.3, options: nil, animations: { self.imageView.center.y += self.view.bounds.height }, completion: nil) } 

这段代码确实可以在屏幕上成功移动图像视图,但我不知道如何让它停在我想要的位置。 它只是滑出屏幕。 目标是将图像从A点移动到B点。如果还有另一种更简单的方法,我很乐意知道。 谢谢!

在viewWillAppear中添加这个,你将得到你的动画,你的问题是autoLayout

 override func viewWillAppear(animated: Bool) { self.imageview.setTranslatesAutoresizingMaskIntoConstraints(true) self.imageview.center.y -= self.view.bounds.height } 

移动视图框架时,最好使用动画antoLayout约束

然后将红色约束拖动到出口 在此处输入图像描述 示例代码

 class ViewController: UIViewController{ @IBOutlet weak var imageview: UIImageView! @IBOutlet weak var yConstraint: NSLayoutConstraint! override func viewWillAppear(animated: Bool) { yConstraint.constant -= self.view.bounds.height; self.view.updateConstraintsIfNeeded() self.view.layoutIfNeeded() } override func viewDidAppear(animated: Bool) { yConstraint.constant += self.view.bounds.height; UIView.animateWithDuration(0.5, delay: 0.3, options: nil, animations: { self.view.layoutIfNeeded() }, completion: nil) } } 
 override func viewDidAppear(animated: Bool) { var destinationY:CGFloat = self.view.bounds.height / 2 // Your destination Y UIView.animateWithDuration(0.5, delay: 0.3, options: nil, animations: { self.imageView.center.y = destinationY }, completion: nil) } 

只是将它移动太远,这将把中心放在屏幕中间。

对于这一行: self.imageView.center.y += self.view.bounds.height

也许让它减少移动? self.imageView.center.y += self.view.bounds.height - 200

你的实现很好,你只需要了解你正在改变的数量。 self.view.bounds.height是指视图控制器视图的高度。 事实上,这可能是相当大的,可能是你正在看的整个屏幕高度。 因此,您需要确定要将其移动到哪一点并进行一些数学运算。 假设您想要在它到达屏幕的上边缘时停止它。 你可以做一些非常简单的事情:

 self.imageView.center.y += (self.view.frame.origin.y - self.imageView.frame.origin.y) 

我没有对此进行测试,请以最适合您情况的方式实施。

 override func viewDidAppear(animated: Bool) { super.viewDidAppear( animated ) let orgY = self.imageView.center.y self.imageView.center.y -= view.bounds.height UIView.animateWithDuration(0.5, delay: 0.3, options: nil, animations: { self.imageView.center.y = orgY }, completion: nil) } 

保存原点并开始!