【问题标题】:UIView.animateWithDuration not animatingUIView.animateWithDuration 没有动画
【发布时间】:2014-11-24 22:08:47
【问题描述】:

我的 ViewController viewDidLoad 函数中的 UIView.animateWithDuration 调用没有动画。立即调用完成块。 println 输出显示“完成真”。 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)")
            })
        }

        // ...
    }
}

【问题讨论】:

    标签: swift ios8 animatewithduration xcode6.0.1


    【解决方案1】:

    您是否尝试将其放入 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() 不是您应该为视图设置动画的理想场所。我在viewDidAppear() 方法中尝试了你的代码,它可以工作。

    【讨论】:

      猜你喜欢
      • 2015-02-23
      • 1970-01-01
      • 1970-01-01
      • 2015-11-20
      • 2016-07-25
      • 2015-09-27
      • 2017-05-24
      • 2021-07-03
      • 1970-01-01
      相关资源
      最近更新 更多