今天我制作了一款没有 SpriteKit
的射击游戏
我在动画期间检查 UIImageView
位置时遇到问题。
我尝试过的代码:
@IBAction func MoveTDButton(_ sender: UIButton) {
animationForPlane = UIViewPropertyAnimator(duration: 0.5, curve: .easeIn, animations: {
if sender.tag == 1 {
self.ship.center.x = sender.frame.origin.x + sender.frame.size.width - (self.ship.frame.size.width / 2)
self.ship.image = #imageLiteral(resourceName: "shipright")
print(self.ship.center.x)
}else{
self.ship.center.x = sender.frame.origin.x + (self.ship.frame.size.width / 2)
self.ship.image = #imageLiteral(resourceName: "shipleft")
print(self.ship.center.x)
}
})
animationForPlane.startAnimation()
}
private func autoshoot(_ imgShip :UIImageView, _ imgBullet :UIImageView){
var t: TimeInterval!
t = 0.2
//var hauteur = self.view.frame.maxY
if let duration = t {
UIView.animate(withDuration: duration, animations: {
imgBullet.center.y = 10
imgBullet.center.x = self.ship.center.x
}, completion: { (true) in
imgBullet.center.x = self.ship.center.x
imgBullet.center.y = imgShip.center.y
//checkPosEnemy(img, hauteur: hauteur)
self.autoshoot(imgShip, imgBullet)
})
}
}
函数MoveTDButton
用于通过按钮(左和右)移动我的船,并带有幻灯片动画。
并且,自动拍摄功能用于拍摄^^
问题:
当我单击移动我的船的按钮时,位置是在动画结束时设置的,而不是在动画期间设置的。 所以我的子弹不是在船的位置而是在动画结束时发射的。
最佳答案
如果使用SpriteKit或者类似的框架会更好。
但是要回答您的问题,您可以尝试:
imgBullet.center.x = self.ship.layer.presentation()!.position
代替
imgBullet.center.x = self.ship.center.x
关于ios - 动画期间 UIImageView 的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45211083/