2017-04-12 22 views
1

我希望能夠用動畫將我的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() 
} 

回答

1

的什麼是目前發生的事情是,你正在編輯的self.playerOne.frame.origin.x值的原因,從中間移動它,您應該編輯視圖的center(在您的案例中爲playerOne)。

不知何故,它可能是類似於:

@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() 
} 
+0

謝謝!完全忘記了中心價值! – Username

+0

很高興幫助 –

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) 
相關問題