2017-04-27 34 views
0

有人可以告訴我爲什麼這不是動畫嗎?我覺得代碼是完美的,但動畫功能不起作用。按鈕是從一邊跳到另一邊,但就像我說它不是動畫...我正在嘗試使用約束動畫我的解釦並且動畫不起作用

func moveResetButton(constraint : NSLayoutConstraint, isOffScreen : Bool) { 

    if isOffScreen { 
     //animate the button onto the screen 
     UIView.animate(withDuration: 3.0, delay: 0.0, options: .curveLinear, animations: { 

      constraint.constant += 120 

     }, completion: nil) 

    } else { 
     //animate the button off of the screen 
     UIView.animate(withDuration: 1.0, delay: 0.0, options: .curveLinear, animations: { 

      constraint.constant -= 120 

     }, completion: nil) 
    } 
} 
+0

我已經給我的回答添加了評論。基本上,你正在改變動畫中的約束*。這是行不通的。您需要(1)更改約束,然後**(**)使變化生效。 – dfd

+0

嘿。是的,我其實是想通了,現在就添加它。我在SO的前一個問題上發現它,並且它在Objective-C中。我不知道我是如何理解它,但它的工作。大聲笑感謝您的努力,但。我希望將來能聽到更多的消息。 :) – AdrianGutierrez

回答

0

我想通了。只需要在約束更改下將self.view.layoutIfNeeded()添加到代碼中。工作順利。

+0

很高興你知道了。聽起來這是一個理解視圖(和視圖控制器)事件序列的問題。 – dfd