从中间而不是左上角移动

时间:2017-04-12 10:04:49

标签: ios swift swift3 uikit

我希望能够通过动画将我的playerOne移动到point2,但是当它移动时,它会从playerOne的左上角移动到point2的左上角。我想知道是否有办法将playerOne从中间而不是左上角移动到point2的中间。

@IBAction func moveToPoint2(_ sender: Any) {
    playerOnePosition = 2
    UIView.animate(withDuration: 1.25, delay: 0, options: .curveLinear, animations: {
        self.playerOne.frame.origin.x = self.point2.frame.origin.x-self.playerOne.frame.height/2
    }, completion: nil)
    pickQuestion()
}

2 个答案:

答案 0 :(得分:1)

当前发生的事情的原因是您正在编辑self.playerOne.frame.origin.x的值,要从中间移动它,您应该编辑视图的centerplayerOne在你的情况下)。

不知何故,它可能类似于:

@IBAction func moveToPoint2(_ sender: Any) {
    playerOnePosition = 2
    UIView.animate(withDuration: 1.25, delay: 0, options: .curveLinear, animations: {
        self.playerOne.frame.center = self.point2.center
    }, completion: nil)
    pickQuestion()
}

答案 1 :(得分:0)

而不是这个

self.playerOne.frame.origin.x = self.point2.frame.origin.x-self.playerOne.frame.height/2

试试这个

self.playerOne.frame = CGRect(x: self.point2.frame.origin.x, y: self.point2.frame.origin.y, width: self.playerOne.frame.size.width, height: self.playerOne.frame.size.height)