2014-09-30 76 views
2

的UIView.animateWithDuration呼叫我的視圖控制器的viewDidLoad功能不動畫。完成塊被立即調用。 println輸出顯示'完成true'。 viewDidLoad函數放錯了我的啓動動畫嗎?任何提示都非常感謝。UIView.animateWithDuration不是動畫

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

     // ... 
    } 
} 

回答

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

     // ... 
    } 
+0

剛剛拉開自己在辦公室 – 2016-02-18 18:29:55

3

viewDidLoad()並不是你應該看動畫的理想場所。我在viewDidAppear()方法中試過了你的代碼,它工作。