简体   繁体   中英

Position of UIImageView during an animation

Today I make a shoot em up game without SpriteKit And I have a problem for check UIImageView position during an animation.

Code I have tried:

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

    }

The function MoveTDButton it's for move my ship by button (left and right) with animation for the slide. And, auto shoot function for shoot ^^

Problem :

When I click on a button for move my ship, position is set at the end of animation and not during. So my bullet is not shoot at the ship's position but at the end of animation.

It will be better if you Use SpriteKit or similar frameworks.

However to answer to your question you can try :

imgBullet.center.x = self.ship.layer.presentation()!.position instead of imgBullet.center.x = self.ship.center.x

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM