ios - 动画期间 UIImageView 的位置

标签 ios swift animation uiimageview

今天我制作了一款没有 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/

相关文章:

ios - 如何从通知中启动 watchOS 应用程序?

ios - 如何在 XIB 中调用 UITextField 的委托(delegate)方法?

ios - 如何将 Label 或 Button 发送到后面?

ios - 使用 AVAudioSequencer 将 MIDI 发送到第三方 AUv3 乐器

ios - 响应 PHPhotoLibraryChangeObserver 事件后更新选定的 collectionview 索引

animation - Flutter 条件动画

ios - UIView transitionWithView : animation behaviour with background color

ios - 突出显示事件的分段控件 + 在事件段周围添加边框

ios - Swift:在 init/deinit 中添加/删除观察者会在 Interface Builder 中引发错误

ios - 如何在 UIButton 上制作原生 "Pulse effect"动画 - iOS