ios Swift 3 UIView 动画
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39489925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift 3 UIView animation
提问by Alex Brown
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.
自从将我的项目升级到 swift 3 以来,我的自动布局约束动画不起作用;更具体地说,它们是捕捉到新位置而不是动画。
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
// ....
})
I know they added the UIViewPropertyAnimator
class but am yet to try it.
我知道他们添加了UIViewPropertyAnimator
课程,但我还没有尝试。
回答by Enoch Ng
I had this problem too with the newest update to swift 3.
我在 swift 3 的最新更新中也遇到了这个问题。
To be exact, whenever you want to animate the view, you actually call layoutIfNeeded on the superview of that view.
确切地说,无论何时您想要为视图设置动画,您实际上都在该视图的超级视图上调用 layoutIfNeeded。
Try this instead:
试试这个:
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
// ....
})
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.
在过去,他们似乎对能够重新布局您想要动画的视图很宽容。但是在文档中,您确实应该在超级视图中调用 layoutIfNeeded 。
回答by Saumil Shah
Upgrade to swift 3.0
升级到 swift 3.0
View right to left animation like apple default push animation
查看从右到左的动画,如苹果默认推送动画
//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)
回答by abdullahselek
First set the new value for your constraint and then call animate.
首先为您的约束设置新值,然后调用 animate。
self.YOUR_CONSTRAINT.constant = NEW_VALUE
UIView.animate(withDuration: 1.0) {
self.view.layoutIfNeeded()
}
回答by Prashant Gaikwad
Swift 3.1 , Xcode 8.3.2
斯威夫特 3.1 , Xcode 8.3.2
This code works well for me. Any changes on your view will animate in slow motion.
这段代码对我很有效。您视图上的任何更改都将以慢动作动画。
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()
})
回答by Shamshad
Swift 4, Xcode 10
斯威夫特 4,Xcode 10
Right to Left animation for search
用于搜索的从右到左动画
//intially set x = Your_View_Width
//初始设置 x = Your_View_Width
viewOfSearch.frame = CGRect(x: viewOfSearch.frame.size.width, y: 0 , width: viewOfSearch.frame.size.width, height: 50)
UIView.animate(withDuration: 0.50, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0, options: [], animations: {
//Set x position what ever you want
self.viewOfSearch.frame = CGRect(x: 0, y: 0 , width: self.viewOfSearch.frame.size.width, height: 50)
}, completion: nil)
回答by Raghib Arshi
func setView(view: UIView) {
UIView.transition(with: view, duration: 1.0, options: .transitionFlipFromTop, animations: {
})
}
- change the option part to apply new animations on that particular UIView.
- 更改选项部分以在该特定 UIView 上应用新动画。