【问题标题】:swift UIView animateWithDuration with repeat and autoreverseswift UIView animateWithDuration 与重复和自动反转
【发布时间】:2016-01-10 19:36:57
【问题描述】:

我是 swift 新手,这是我有史以来的第一个问题.... 我想缩小一个持续时间为 2 秒的球,然后将其扩大 5 秒。 我的问题是第二个持续时间被忽略(球缩小 2 秒并增长 2 秒)。 我希望有人可以帮助我。

这是我的尝试:

    let ball = UIView()
    ball.frame = CGRectMake(50, 50, 50, 50)
    ball.backgroundColor = UIColor.blueColor()
    ball.layer.cornerRadius=25
    relaxContainer.addSubview(ball)

    UIView.animateWithDuration(2.0, delay:0, options: [.Repeat, .Autoreverse], animations: {
        ball.frame = CGRectMake(50, 50, 20, 20)
        }, completion: { finished in
            UIView.animateWithDuration(5.0, animations: {
                ball.frame = CGRectMake(50, 50, 50, 50)
            })
    })

【问题讨论】:

    标签: ios swift animation animatewithduration


    【解决方案1】:

    感谢 Matt 的帮助,我的答案(持续时间、原始问题的变量已更改):

    斯威夫特 2

    let duration = 6.0
    let delay = 0.0
    UIView.animateKeyframesWithDuration(duration, delay: delay, options: [.Repeat], animations: {
        UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 1/3, animations: {
            ball.frame = CGRectMake(screenWidth/8*3, screenHeight/8*3, screenWidth/4, screenWidth/4)
        })
        UIView.addKeyframeWithRelativeStartTime(1/3, relativeDuration: 2/3, animations: {
            ball.frame = CGRectMake(screenWidth/4, screenHeight/4, screenWidth/2, screenWidth/2)
        })
        }, completion: nil
    )
    

    斯威夫特 3、4、5

    let duration = 6.0
    let delay = 0.0
    UIView.animateKeyframes(withDuration: duration, delay: delay, options: [.repeat], animations: {
        UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 1/3, animations: {
            ball.frame = CGRect(x: screenWidth/8*3, y: screenHeight/8*3, width: screenWidth/4, height: screenWidth/4)
        })
        UIView.addKeyframe(withRelativeStartTime: 1/3, relativeDuration: 2/3, animations: {
            ball.frame = CGRect(x: screenWidth/4, y: screenHeight/4, width: screenWidth/2, height: screenWidth/2)
        })
        }, completion: nil
    )
    

    【讨论】:

    • 如果你不使用它,记得把你的完成块清零,以保持你的代码干净:)
    猜你喜欢
    • 2013-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-02
    • 1970-01-01
    相关资源
    最近更新 更多