Swift3 UIView动画
Swift 3 UIView animation
自从将我的项目升级到 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
// ....
})
我知道他们添加了 UIViewPropertyAnimator
class 但我还没有尝试。
我在 swift 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
// ....
})
在过去,他们似乎对能够重新布局您想要动画的视图一直很宽容。但是在文档中,您真的应该在超级视图中调用 layoutIfNeeded。
升级到 swift 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)
首先为您的约束设置新值,然后调用动画。
self.YOUR_CONSTRAINT.constant = NEW_VALUE
UIView.animate(withDuration: 1.0) {
self.view.layoutIfNeeded()
}
Swift 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()
})
Swift4,Xcode10
从右到左的搜索动画
//初始设置 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)
func setView(view: UIView) {
UIView.transition(with: view, duration: 1.0, options: .transitionFlipFromTop, animations: {
})
}
- 更改选项部分以在该特定 UIView 上应用新动画。
自从将我的项目升级到 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
// ....
})
我知道他们添加了 UIViewPropertyAnimator
class 但我还没有尝试。
我在 swift 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
// ....
})
在过去,他们似乎对能够重新布局您想要动画的视图一直很宽容。但是在文档中,您真的应该在超级视图中调用 layoutIfNeeded。
升级到 swift 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)
首先为您的约束设置新值,然后调用动画。
self.YOUR_CONSTRAINT.constant = NEW_VALUE
UIView.animate(withDuration: 1.0) {
self.view.layoutIfNeeded()
}
Swift 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()
})
Swift4,Xcode10
从右到左的搜索动画
//初始设置 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)
func setView(view: UIView) {
UIView.transition(with: view, duration: 1.0, options: .transitionFlipFromTop, animations: {
})
}
- 更改选项部分以在该特定 UIView 上应用新动画。