2017-10-08 42 views
0

我在視圖上有一個logoImage,動畫約束。 動畫的約束條件是寬度和高度;從原來的狀態220.0到240.0(正常工作),然後我想使它回到220.0,但動畫被跳過,圖像直接返回到0.1秒到220.0。Swift跳過一個動畫(UIView.animate)

這裏是動畫的代碼預先的任何幫助,我能得到

func animate() { 

     self.imageHeightConstraint.constant = 240.0 
     self.imageWidthConstraint.constant = 240.0 

     UIView.animate(withDuration: 1.0, 
         delay:0.0, 
         options: .curveEaseIn, 
         animations:{ 

         self.logoImage.layoutIfNeeded() 

     }, completion: { (finished: Bool) -> Void in 

      self.imageHeightConstraint.constant = 220.0 
      self.imageWidthConstraint.constant = 220.0 



      UIView.animate(withDuration: 2.0, 
          delay:0.0, 
          options: .curveEaseIn, 
          animations: { 

       self.logoImage.layoutIfNeeded() 

      }) 

     }) 

    } 

謝謝! ;-)

回答

1

您需要在完成塊內調用self.logoImage.setNeedsLayout(),然後重新設置常量。這是因爲您需要使接收器的當前佈局無效並在下一個更新週期中觸發佈局更新。

試試這個代碼,而不是,它會爲你工作:

func animate() { 
    self.imageHeightConstraint.constant = 240.0 
    self.imageWidthConstraint.constant = 240.0 
    UIView.animate(withDuration: 1.0, 
        delay:0.0, 
        options: .curveEaseIn, 
        animations:{ 

        self.logoImage.layoutIfNeeded() 

    }, completion: { (finished: Bool) -> Void in 
     self.logoImage.setNeedsLayout() 
     self.imageHeightConstraint.constant = 220.0 
     self.imageWidthConstraint.constant = 220.0 

     UIView.animate(withDuration: 2.0, 
         delay:0.0, 
         options: .curveEaseIn, 
         animations: { 

      self.logoImage.layoutIfNeeded() 

     }) 
    }) 
} 
+0

工作!非常感謝 ;) – Julien