UIView.animateWithDuration not animating

邮差的信 提交于 2019-12-24 01:01:35

问题


The UIView.animateWithDuration call in my ViewController viewDidLoad function does not animate. The completion block gets called immediately. The println output shows 'completion true'. Is the viewDidLoad function the wrong place to put my startup animation? Any hints are greatly appreciated.

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)")
            })
        }

        // ...
    }
}

回答1:


Have you tried putting it in ViewDidAppear?

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    if let led = beatIndicator {
        led.alpha = 1.0
        led.frame = CGRect(x: 20, y: 20, width: 30, height: 30)
        led.layer.cornerRadius = 15

        UIView.animate(withDuration: 3.0, animations:{
            led.alpha = 0.5
            led.frame = CGRect(x: 25, y: 25, width: 20, height: 20)
            led.layer.cornerRadius = 10
        }, completion:{(value: Bool) in
            print("completion \(value)")
        })
    }

    // ...
}



回答2:


viewDidLoad() is not the ideal place where you should animate views. I tried your code in the viewDidAppear() method and it works.



来源:https://stackoverflow.com/questions/26118141/uiview-animatewithduration-not-animating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!