UIView.animateWithDuration没有动画

时间:2014-09-30 10:08:19

标签: swift ios8 animatewithduration xcode6.0.1

我的ViewController viewDidLoad函数中的UIView.animateWithDuration调用没有动画。立即调用完成块。 println输出显示'完成true'。 viewDidLoad函数是否放置了我的启动动画的错误位置?任何提示都非常感谢。

class ViewController: UIViewController {
    @IBOutlet var beatIndicator:UIView?

    override func viewDidLoad() {
        super.viewDidLoad()

        if let led = beatIndicator? {
            led.alpha = 1.0
            led.frame = CGRectMake(20, 20, 30, 30)
            led.layer.cornerRadius = 15

            UIView.animateWithDuration(3.0, animations:{
                led.alpha = 0.5
                led.frame = CGRectMake(25, 25, 20, 20)
                led.layer.cornerRadius = 10
            }, completion:{(value: Bool) in
                println("completion \(value)")
            })
        }

        // ...
    }
}

2 个答案:

答案 0 :(得分:7)

您是否尝试将其放入ViewDidAppear?

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        if let led = beatIndicator? {
            led.alpha = 1.0
            led.frame = CGRectMake(20, 20, 30, 30)
            led.layer.cornerRadius = 15

            UIView.animateWithDuration(3.0, animations:{
                led.alpha = 0.5
                led.frame = CGRectMake(25, 25, 20, 20)
                led.layer.cornerRadius = 10
            }, completion:{(value: Bool) in
                println("completion \(value)")
            })
        }

        // ...
    }

答案 1 :(得分:3)

viewDidLoad()不是理想的观看地点。我在viewDidAppear()方法中尝试了您的代码,但它确实有效。