Swift 3 UIView动画 [英] Swift 3 UIView animation

查看:206
本文介绍了Swift 3 UIView动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从将项目升级到3步后,我的自动布局约束动画不起作用;更具体地说,它们是在捕捉新位置而不是设置动画.

Since upgrading my project to swift 3 my autolayout constraint animations aren't working; to be more specific, they're snapping to the new position rather than animating.

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类,但尚未尝试.

I know they added the UIViewPropertyAnimator class but am yet to try it.

推荐答案

我对Swift 3的最新更新也遇到了这个问题.

I had this problem too with the newest update to swift 3.

确切地说,每当您要为视图设置动画时,您实际上都在该视图的超级视图上调用layoutIfNeeded.

To be exact, whenever you want to animate the view, you actually call layoutIfNeeded on the superview of that view.

尝试以下方法:

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
// ....
})

过去,他们似乎对能够重新传递您要设置动画的视图很宽容.但是在文档中,您实际上应该在超级视图中调用layoutIfNeeded.

It seems in the past they've been lenient about being able to just relayout the view you want to animate. But its in the documentation that you should really be calling layoutIfNeeded in the superview.

这篇关于Swift 3 UIView动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆