首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >迅捷-动画方向

迅捷-动画方向
EN

Stack Overflow用户
提问于 2018-01-12 02:02:37
回答 1查看 1.7K关注 0票数 1

我有一个简单的问题做恶梦。在我的应用程序中,我有云在后台移动(目前从左到右)。

然而,有了背景,他们需要从右到左。

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

代码语言:javascript
运行
复制
@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)
    })
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-12 02:07:38

如果您想将它们从右移到左,那么只需更改起始和结束x原点。

代码语言:javascript
运行
复制
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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48218535

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档