Ios 从中间移动,而不是从左上角
我希望能够通过动画将我的playerOne移动到point2,但当它移动时,它会从playerOne的左上角移动到point2的左上角。我想知道是否有一种方法可以将玩家从中间移动,而不是从左上角移动到第二点的中间Ios 从中间移动,而不是从左上角,ios,swift,swift3,uikit,Ios,Swift,Swift3,Uikit,我希望能够通过动画将我的playerOne移动到point2,但当它移动时,它会从playerOne的左上角移动到point2的左上角。我想知道是否有一种方法可以将玩家从中间移动,而不是从左上角移动到第二点的中间 @IBAction func moveToPoint2(_ sender: Any) { playerOnePosition = 2 UIView.animate(withDuration: 1.25, delay: 0, options: .curveLinear,
@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()
}
而不是这个
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)
当前发生这种情况的原因是您正在编辑
self.playeron.frame.origin.x的值,要从中间移动它,您应该编辑视图的值(在您的示例中是playeron
)
不知何故,它可能类似于:
@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()
}
谢谢完全忘记了中心值!很乐意帮忙