2016-09-14 106 views
16

由於將我的項目升級到swift 3我的自動佈局約束動畫不起作用;更具體地說,他們正在尋找新的位置而不是動畫。Swift 3 UIView動畫

UIView.animate(withDuration: 0.1, 
       delay: 0.1, 
       options: UIViewAnimationOptions.curveEaseIn, 
       animations: {() -> Void in 
        constraint.constant = ButtonAnimationValues.YPosition.DefaultOut() 
        self.layoutIfNeeded() 
    }, completion: { (finished) -> Void in 
    // .... 
}) 

我知道他們添加的UIViewPropertyAnimator類,但我沒有嘗試。

+1

我一直在尋找最近的解決方案。許多人都有同樣的問題,即使使用新的UIViewPropertyAnimator,我也無法使其工作。也許這是iOS 10中未解決的錯誤。 – diegotrevisan

+0

您是否嘗試在動畫調用之前設置常量? – lkraider

+0

@lkraider是的,已經嘗試過。 –

回答

21

我有這個問題太與最新更新迅速3.

確切的說,只要你想動畫視圖,你居然叫layoutIfNeeded對這一觀點的上海華。

試試這個:

UIView.animate(withDuration: 0.1, 
      delay: 0.1, 
      options: UIViewAnimationOptions.curveEaseIn, 
      animations: {() -> Void in 
       constraint.constant = ButtonAnimationValues.YPosition.DefaultOut() 
       self.superview?.layoutIfNeeded() 
}, completion: { (finished) -> Void in 
// .... 
}) 

看來,在過去他們一直寬鬆些能夠重新佈局只是要進行動畫視圖。但是它在文檔中應該真的在superview中調用layoutIfNeeded。

+0

職位正在改變,但沒有動畫。 – Satyam

+3

請嘗試此代碼,其工作對我來說。 UIView.animate(withDuration:1,延遲:0,選擇:.curveEaseInOut,動畫:{ self.bottomViewTopConstraint.constant = 0.0 self.bottomView.superview .layoutIfNeeded() ?},完成:無) – jbchitaliya

+0

哇感謝。偉大的解決方案,和一個奇怪的問題。 – Siriss

7

升級到3.0迅速

查看從右到左的動畫像蘋果默認的普通動畫

//intially set x = SCREEN_WIDTH 
view.frame = CGRect(x: ScreenSize.SCREEN_WIDTH, y: ScreenSize.SCREEN_HEIGHT - heightTabBar , width: ScreenSize.SCREEN_WIDTH, height: heightTabBar) 

UIView.animate(withDuration: 0.50, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0, options: [], animations: { 
//Set x position what ever you want 
         view.frame = CGRect(x: 0, y: ScreenSize.SCREEN_HEIGHT - heightTabBar , width: ScreenSize.SCREEN_WIDTH, height: heightTabBar) 

        }, completion: nil) 
5

首先爲您約束的新值,然後調用動畫。

self.YOUR_CONSTRAINT.constant = NEW_VALUE 
UIView.animate(withDuration: 1.0) { 
    self.view.layoutIfNeeded() 
} 
0

雨燕3.1的Xcode 8.3.2

此代碼的工作很適合我。視圖上的任何更改都會以慢動作呈現動畫效果。

UIView.animate(withDuration: 3.0, animations: { // 3.0 are the seconds 

// Write your code here for e.g. Increasing any Subviews height. 

self.view.layoutIfNeeded() 

})