Ios 动画期间UIImageView的位置

Ios 动画期间UIImageView的位置,ios,swift,animation,uiimageview,Ios,Swift,Animation,Uiimageview,今天我做了一个没有SpriteKit的射击游戏 我在动画中检查UIImageView位置时遇到问题 我尝试过的代码: @IBAction func MoveTDButton(_ sender: UIButton) { animationForPlane = UIViewPropertyAnimator(duration: 0.5, curve: .easeIn, animations: { if sender.tag == 1 {

今天我做了一个没有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
用于移动我的发货按钮(左和右),并带有幻灯片动画。 并具有自动拍摄功能^^

问题:

@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)
            })
        }

    }
当我单击“移动我的飞船”按钮时,位置设置在动画结束时,而不是在动画期间。
因此,我的子弹不是在船的位置射击,而是在动画结束时射击。

如果使用SpriteKit或类似的框架,效果会更好

但是,要回答您的问题,您可以尝试:

imgBullet.center.x=self.ship.layer.presentation()!。位置
而不是
imgBullet.center.x=self.ship.center.x

考虑到你是在编写游戏而不是一些静态动画,你是否应该通过玩家的当前动作每帧更新一次船的位置,以检查其他活动,如被敌人击中或与地形碰撞?这将使你也能利用这个位置来放置子弹进行射击。我同意本·翁的观点。游戏编程的最佳实践是遵循帧循环,逐帧更新对象的位置。SpriteKit已经为此实施了游戏周期。我建议迁移到spriteKit。