2016-09-14 80 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() 

})