[英]Animating UIStackView arrangedSubview content size change
当其内在内容大小发生变化时,是否可以为已排列的子视图设置动画?
例如,假设我有一个排列好的子视图,其中包含一个固定到边缘的 UILabel。 那个 label 有少量文字。 新文本出现,比以前的文本大。 label 的固有内容大小现在更大了。
我希望能够像这样制作动画:
stackView.layoutIfNeeded()
self.label.text = self.expanded ? textTwo : textOne
UIView.animateWithDuration(0.3) { () -> Void in
self.stackView.layoutIfNeeded()
}
堆栈视图当前从旧内容大小“跳转”到新内容大小,忽略 animation 块。
当然,另一种方法是手动找出排列的子视图的高度,并为该约束设置动画。 如果可能的话,我想避免这种情况。
找到了行之有效的方法:
label.text = expanded ? textOne : textTwo
UIView.animateWithDuration(0.4) { () -> Void in
self.stackView.setNeedsLayout()
self.stackView.layoutIfNeeded()
}
不起作用:
label.text = expanded ? textOne : textTwo
UIView.animateWithDuration(0.4) { () -> Void in
self.stackView.layoutIfNeeded()
}
奇怪的是,更改固有内容的大小并不会使stackView想要布局...
假设您有一个自定义 UI 元素并更改它的intrinsicContentSize
...
var stackView: UIStackView!
var yourUIElement: UIElement! // (any kind of ui element)
UIView.animate(withDuration: 3, delay: 0) { [weak self] in
guard let self = self else {return}
self.yourUIElement.configure() // your configuration
// in your case this will be label.text = expanded ? textOne : textTwo
self.stackView.layoutIfNeeded()
self.stackView.setNeedsLayout()
self.yourUIElement.invalidateIntrinsicContentSize()
// in your case this will be self.label.invalidateIntrinsicContentSize()
}
这对我有用,希望对你有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.