SWIFT 更改 NSLayoutConstraint 时的动画
SWIFT animation while changing NSLayoutConstraint
这是我的代码:
func scrollViewDidScroll(scrollView: UIScrollView) {
if mapHeight.constant < 400 {
mapHeight.constant = 400
tableOfRestaraunts.reloadData()
}
}
func normalSize() {
if mapHeight.constant == 400 {
mapHeight.constant = 200
}
}
当用户滚动 UITableView 时,我的地图变小,当他触摸任何地方时,它变成默认大小。
我想添加一些动画(这样看起来更流畅)。我已经尝试了一些来自网络的解决方案,但它们没有帮助。有什么建议吗?
一如既往,您应该调用 layoutIfNeeded
来动画化布局约束的变化。在您的情况下,您应该在包含约束的视图上调用它(这对我来说不是很明显)。或者在实际动画块中简单 self.view.layoutIfNeeded()
:
该示例为您制作动画,动画需要 0.5
秒才能完成。您可以使用 UIView
's animation blocks for a better control on your animations' details.
的重载
func scrollViewDidScroll(scrollView: UIScrollView) {
if mapHeight.constant < 400 {
UIView.animateWithDuration(0.5) { () -> Void in
mapHeight.constant = 400
self.view.layoutIfNeeded()
}
tableOfRestaraunts.reloadData()
}
}
func normalSize() {
if mapHeight.constant == 400 {
UIView.animateWithDuration(0.5) { () -> Void in
mapHeight.constant = 200
self.view.layoutIfNeeded()
}
}
}
更新:我不知道你没有动画块。已更新以包含实际的动画块。
这是我的代码:
func scrollViewDidScroll(scrollView: UIScrollView) {
if mapHeight.constant < 400 {
mapHeight.constant = 400
tableOfRestaraunts.reloadData()
}
}
func normalSize() {
if mapHeight.constant == 400 {
mapHeight.constant = 200
}
}
当用户滚动 UITableView 时,我的地图变小,当他触摸任何地方时,它变成默认大小。
我想添加一些动画(这样看起来更流畅)。我已经尝试了一些来自网络的解决方案,但它们没有帮助。有什么建议吗?
一如既往,您应该调用 layoutIfNeeded
来动画化布局约束的变化。在您的情况下,您应该在包含约束的视图上调用它(这对我来说不是很明显)。或者在实际动画块中简单 self.view.layoutIfNeeded()
:
该示例为您制作动画,动画需要 0.5
秒才能完成。您可以使用 UIView
's animation blocks for a better control on your animations' details.
func scrollViewDidScroll(scrollView: UIScrollView) {
if mapHeight.constant < 400 {
UIView.animateWithDuration(0.5) { () -> Void in
mapHeight.constant = 400
self.view.layoutIfNeeded()
}
tableOfRestaraunts.reloadData()
}
}
func normalSize() {
if mapHeight.constant == 400 {
UIView.animateWithDuration(0.5) { () -> Void in
mapHeight.constant = 200
self.view.layoutIfNeeded()
}
}
}
更新:我不知道你没有动画块。已更新以包含实际的动画块。