Reputation: 27
I wanted to be able to move my playerOne to point2 with an animation but when it moves it moves from the top left corner of playerOne to the top left corner of point2. I was wondering if there was a way to move playerOne from the middle instead of the top left corner to the middle of 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()
}
Upvotes: 1
Views: 201
Reputation: 31645
The reason of what is currently happening is that you are editing the value of self.playerOne.frame.origin.x
, for moving it from the middle, you should edit the center
of the view (which is playerOne
in your case).
Somehow, it might be similar to:
@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()
}
Upvotes: 1
Reputation: 1068
Instead of this
self.playerOne.frame.origin.x = self.point2.frame.origin.x-self.playerOne.frame.height/2
Try This
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)
Upvotes: 0