swift - 如何获得不同的插入和删除过渡动画?

标签 swift swiftui

我可以为插入 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/

相关文章:

ios - UIPageControl 不滑动

ios - Swift - 自定义 Segue 错误( 'view' 的使用不明确)

swift - 如何获取自定义形状的 .onHover(...) 事件

xcode - SwiftUI 错误 - 列表更改锁定 UI -(旧标题 : SwiftUI CoreData fetch is very slow)

ios - 检查哪个 ViewController 进行了 segue(来自目标 ViewController)

ios - 如何将 url 数组发送到另一个 View 并转换为图像?

swift - 对类型推断背后机制的猜测

ios - 我们可以使用真实设备查看 swift UI 预览吗?

ios - ScrollView 中的 SwiftUI 动画/过渡表现奇怪

xcode - 如何使用 osascript 在 SwiftUI 中以管理员权限运行 shell 脚本