【问题标题】:UIView animation skips first animationUIView 动画跳过第一个动画
【发布时间】:2015-01-15 01:33:08
【问题描述】:

对此很抱歉!

一旦我运行按下按钮“按下”UIView“背景”的背景颜色立即变为蓝色,然后动画为紫色,完全跳过动画到黄色,然后到蓝色。

我做错了什么?

  @IBAction func Press(sender: AnyObject) {

UIView.animateWithDuration(5, animations: {
self.Background.backgroundColor = UIColor.yellowColor()
self.Background.backgroundColor = UIColor.blueColor()
self.Background.backgroundColor = UIColor.purpleColor()
}, completion:{(Bool)  in
println("COLOR CHANGED")
})
}

【问题讨论】:

  • 顺便说一句,这段代码是错误的:(Bool) in。它不会抛出错误,但不会像您认为的那样做。只需写_ in
  • 并且停止在函数名和变量名中使用大写字母!大写字母用于类名。

标签: ios swift uiview


【解决方案1】:

您不能在单个 UIView.animateWithDuration 调用中为同一属性设置多个状态更改的动画。它只会动画到最后的状态变化(就像你的情况一样)。相反,您可以使用 completionBlock 将它们链接在一起。

UIView.animateWithDuration(5/3.0, animations: {

    self.view.backgroundColor = UIColor.yellowColor()

    }, completion:{ finished1 in
        UIView.animateWithDuration(5/3.0, animations: {

            self.view.backgroundColor = UIColor.blueColor()

            }, completion:{finished2  in
                UIView.animateWithDuration(5/3.0, animations: {

                    self.view.backgroundColor = UIColor.purpleColor()

                    }, completion:{finished3  in

                        println("COLOR CHANGED")
                })
        })
})

或者您可以使用关键帧动画,指定中间帧,如下所示。 relativeDuration 应该是一个介于 0 和 1 之间的值,表示一个关键帧的相对持续时间。例如,如果整个动画是3 seconds 并且relativeDuration 是(1/3),那么该关键帧将动画3/3 = 1 秒。

relativeStartTime 类似地是关键帧开始之后相对于整个动画持续时间的相对时间。例如,如果整个动画是3 seconds,relativeStartTime 是(1/3),那么该关键帧将在1 second 之后开始

var duration = 5.0;
var relativeDuration = 1.0/3;

UIView.animateKeyframesWithDuration(duration, delay: 0, options: nil, animations: {
    UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: relativeDuration, animations: {
        self.view.backgroundColor = UIColor.yellowColor()
    })
    UIView.addKeyframeWithRelativeStartTime(relativeDuration, relativeDuration: relativeDuration, animations: {
        self.view.backgroundColor = UIColor.blueColor()
    })
    UIView.addKeyframeWithRelativeStartTime(2 * relativeDuration, relativeDuration: relativeDuration, animations: {
        self.view.backgroundColor = UIColor.purpleColor()

    })
    }, completion:nil);

【讨论】:

    【解决方案2】:

    对,因为这就是 one 更改为 one 属性的全部内容。您需要制作这三个连续不同的动画。首先动画为黄色;结束后,现在制作一个全新的蓝色动画;然后制作一个全新的紫色动画。

    链接它们的最简单方法是将每个新动画放入前一个动画的完成处理程序中。像这样(你需要改变一些其他的东西,因为我拒绝编写函数和变量以大写字母开头的代码):

    @IBAction func press(sender: AnyObject) {
        UIView.animateWithDuration(5.0/3.0, animations: {
            self.background.backgroundColor = UIColor.yellowColor()
            }, completion:{(Bool)  in
                UIView.animateWithDuration(5.0/3.0, animations: {
                    self.background.backgroundColor = UIColor.blueColor()
                    }, completion:{(Bool)  in
                        UIView.animateWithDuration(5.0/3.0, animations: {
                            self.background.backgroundColor = UIColor.purpleColor()
                            }, completion:{(Bool)  in
                                println("COLOR CHANGED")
                        })
                })
        })
    }
    

    在 iOS 8 上还有一种更优雅(但更难)的方法,即使用关键帧动画。但要开始,我建议你先用简单的方法来做!

    【讨论】:

    • 感谢您的回答!我怎样才能让所有这些代码无限重复自己?
    • 就个人而言,我不会这样做。我会使用关键帧动画并将其设置为无限重复。或者甚至在 Core Animation 的层面上做整个事情;请参阅我的关于如何做到这一点的书:apeth.com/iOSBook/ch17.html#_core_animation
    猜你喜欢
    • 2012-08-31
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多