UIView.animate 和 CGRect 不起作用 - Swift 4

UIView.animate and CGRect don't work - Swift 4

我正在制作这个应用程序,当你按下一个按钮时,角色会上升,然后它会下降。(我希望它表现得像这样:当它被按下时,它必须上升 200y,然后它开始下降) 这是我的代码:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var TheCharacter: UIImageView!
    @IBOutlet weak var TheRocket: UIImageView!

    @IBAction func TheCharacterJumps(_ sender: Any) {

        let TheCharacterY = self.TheCharacter.frame.origin.y
        UIView.animate(withDuration: 0.35, animations: {
            self.TheCharacter.frame = CGRect(x: 167, y: TheCharacterY - 194, width: 40, height: 40)
        }) { (finished) in

        }
        UIView.animate(withDuration: 2, animations: {
            self.TheCharacter.frame = CGRect(x: 167, y: TheCharacterY + 435, width: 40, height: 40)
        }) { (finished) in

        }
    }
}

所以它是这样的: 按下按钮时,角色上升,然后开始下降。 但是当按钮连续按下两次或更多次时,它不会上升 200 年,而只会上升 50 年。 我怎样才能防止这种情况发生?

如果您想要 "flappy" 动画,您应该使用动态动画师并向场景添加引力并向元素添加力。然后 UIKit 会计算动画。

A​​pple 文档是:UIDynamicAnimator.

在这种情况下,每次按下button/screen都会给视图增加一些向上的力,它会自己上升,然后在失去速度后开始下降。

看Ray Wenderlich教程:click

如果你打算用这种用法来实现游戏,也许最好看看 SpriteKit instead of UIKit. Take a look at SKSpriteNode 以及如何添加 SKPhysicsBody。