斯威夫特 - 动画方向



我做了一个简单的噩梦。在我的应用程序上,我有云在后台移动(目前从左到右(。

但是,对于背景,它们需要从右到左。

编辑下面的更多页面代码。希望这会更容易找出我哪里出错了。

@IBOutlet var cloud1: UIImageView!
@IBOutlet var cloud2: UIImageView!
@IBOutlet var cloud3: UIImageView!
@IBOutlet var cloud4: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}
override func viewWillAppear(_ animated: Bool) {
    cloud1.alpha = 0.0
    cloud2.alpha = 0.0
    cloud3.alpha = 0.0
    cloud4.alpha = 0.0
}
override func viewDidAppear(_ animated: Bool) {

    UIView.animate(withDuration: 2.0, delay: 0.1,
                   options: [],
                   animations: {
                    self.cloud1.alpha = 1.0
    }, completion: nil)
    UIView.animate(withDuration: 2.0, delay: 0.1,
                   options: [],
                   animations: {
                    self.cloud2.alpha = 1.0
    }, completion: nil)
    UIView.animate(withDuration: 2.0, delay: 0.1,
                   options: [],
                   animations: {
                    self.cloud3.alpha = 1.0
    }, completion: nil)
    UIView.animate(withDuration: 2.0, delay: 0.1,
                   options: [],
                   animations: {
                    self.cloud4.alpha = 1.0
    }, completion: nil)
    animateTheClouds(cloud: cloud1)
    animateTheClouds(cloud: cloud2)
    animateTheClouds(cloud: cloud3)
    animateTheClouds(cloud: cloud4)
}
func animateTheClouds(cloud : UIImageView) {
    let cloudMovingSpeed = 60.0/view.frame.size.width
    let duration = (view.frame.size.width - cloud.frame.origin.x) * cloudMovingSpeed
    UIView.animate(withDuration: TimeInterval(duration), delay: 0.0, options: .curveLinear, animations: {
        // Animate the origin to be off the left side of the screen.
        cloud.frame.origin.x = cloud.frame.size.width
    }, completion: {_ in
        // Reset back to the right edge of the screen
        cloud.frame.origin.x = -self.view.frame.size.width
        self.animateTheClouds(cloud: cloud)
    })
如果要将它们

从右向左移动,则只需更改开始和结束x原点即可。

func animateTheClouds(cloud : UIImageView) {
    let cloudMovingSpeed = 60.0/view.frame.size.width
    let duration = (cloud.frame.origin.x + cloud.frame.size.width) * cloudMovingSpeed
    UIView.animate(withDuration: TimeInterval(duration), delay: 0.0, options: .curveLinear, animations: {
        // Animate the origin to be off the left side of the screen.
        cloud.frame.origin.x = -cloud.frame.size.width
    }, completion: {_ in
        // Reset back to the right edge of the screen
        cloud.frame.origin.x = self.view.frame.size.width
        self.animateTheClouds(cloud: cloud)
    })

还要确保初始 x 原点设置为 self.view.frame.size.width

最新更新