我正在尝试在具有多行文本的 Text
上添加展开/折叠动画,但遇到了奇怪的问题。
下面是一个GIF,显示了这个问题。我设置了缓慢的动画以使其更清晰。
https://www.dropbox.com/s/sx41g9tfx4hd378/expand-collapse-stack_overflow.gif
我正在对视图的高度属性进行动画处理,似乎无论动画周期如何,Text
将立即转换为一行。以下是一些代码:
struct ContentView: View {
@State var expanded = false
var body: some View {
VStack(spacing: 20) {
HStack {
Button(expanded ? "Colapse" : "Expand") {
withAnimation {
self.expanded.toggle()
}
}
}
VStack(spacing: 10) {
Text(bigText)
Text(bigText)
}
.frame(height: expanded ? .none : 0)
.clipped()
.background(Color.red)
Text("Thist is another text underneath the huge one. ")
.font(.system(.headline))
.foregroundColor(.red)
Spacer()
}
}
}
我尝试过很多其他的东西,目前这是最接近所需输出的方式,就像在UIKit中的
UIStackView
中动画标签一样。有没有正确的方法?这是一个bug吗?通常问题出在开发者身上,但我注意到如果我使用DisclosureGroup
,当它展开时动画可以正常工作,但当它折叠时就没有动画了。所以这可能是多行Text
的限制吗?非常感谢你。
.fixedSize(horizontal: false, vertical: true)
吗? - Lae