我可以为插入 View 和删除 View 时指定不同的转换:
.transition(
.asymmetric(
insertion: .move(edge: .leading)
removal: .move(edge: .trailing)
)
)
这很好用。然而,我希望它在添加时能够快速地缓入,在移除时缓慢地缓出。尽管编译得很好,但以下内容似乎没有执行任何操作。
.transition(
.asymmetric(
insertion: .move(edge: .leading)
.animation(.easeIn(duration: 2)),
removal: .move(edge: .trailing)
.animation(.easeOut(duration: 5))
)
)
我是否误解了使用 .animation
作为过渡修饰符?
演示代码:
struct TestView: View {
@State private var showDetails = false
var body: some View {
VStack {
Button("Press to show details") {
withAnimation {
showDetails.toggle()
}
}
if showDetails {
Text("Details go here.")
.background(Color.blue)
.transition(
.asymmetric(
insertion: .move(edge: .leading)
.animation(.easeIn(duration: 5)),
removal: .move(edge: .trailing)
.animation(.easeOut(duration: 5))
)
)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
最佳答案
这是我发现有用的东西。对每个方向使用不同的 withAnimation
调用:
Button("Press to show details") {
if !showDetails {
withAnimation(.easeIn(duration: 2)) {
showDetails.toggle()
}
} else {
withAnimation(.easeOut(duration: 5)) {
showDetails.toggle()
}
}
}
关于swift - 如何获得不同的插入和删除过渡动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73049760/