[英]How to animate a Text view in SwiftUI?
I would like to animate a Text view whenever it is conditionally presented or removed based upon a state variable.我想在基于 state 变量有条件地呈现或删除文本视图时为其设置动画。
I've tried, to no avail我试过了,没有用
struct AnimatedText: View {
@State var showingText = false
var body: some View {
VStack {
if showingText {
Text("Hello world")
.animation(.easeInOut, value: showingText)
}
Button("Toggle") {
showingText.toggle()
}
}
}
}
How can I animate the Text view?如何为文本视图设置动画?
Appear/disappear is animated by container, so you need to place Text
into some container and make it animatable, like出现/消失是由容器动画的,因此您需要将Text
放入某个容器并使其具有动画效果,例如
var body: some View {
VStack {
VStack {
if showingText {
Text("Hello world")
}
}.animation(.easeInOut, value: showingText) // << here !!
Button("Toggle") {
showingText.toggle()
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.