当用户按下按钮显示矩形时,文本的偏移动画不应被 .smooth(duration: 0.25) 覆盖,而是继续其 50 秒的线性动画。
以下是完整的示例。
struct ContentView: View {
@State private var showingRect = false
var body: some View {
VStack {
Button("Show Rect") {
withAnimation(.smooth(duration: 0.25)) {
showingRect.toggle()
}
}
TextView()
if showingRect {
Rectangle()
.foregroundStyle(.blue)
.frame(width: 100, height: 100)
}
}
.padding()
}
}
struct TextView: View {
@State private var animate = false
var body: some View {
Text("This is a very long text. Yeah, it is very very long.")
.lineLimit(1)
.offset(x: animate ? -300 : 0)
.onAppear {
withAnimation(.linear(duration: 50)) {
animate = true
}
}
}
}