【问题标题】:How to create animation with delay when dismiss view swiftui如何在关闭视图swiftui时延迟创建动画
【发布时间】:2020-03-26 01:48:57
【问题描述】:

我在我的代码中使用了以下示例(iOS SwiftUI: pop or dismiss view programmatically),但我不知道如何创建一个动画,就像翻页一样,并在点击 [Button] 时延迟几秒钟。有人知道解决方案吗?

struct DetailView: View {
    @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
    var body: some View {
        Button(
            "Here is Detail View. Tap to go back.",
            action: {

                //withAnimation(.linear(duration: 5).delay(5))// Error occurred in dalay.(Type of expression is ambiguous without more context)
                withAnimation(.linear(duration: 5)) // not work 
                {
                    self.presentationMode.wrappedValue.dismiss()
                }
        }
        )
    }
}

struct RootView: View {
    var body: some View {
        VStack {
            NavigationLink(destination: DetailView())
            { Text("I am Root. Tap for Detail View.")
        }
    }
}

struct ContentView: View {
    var body: some View {
        NavigationView {
            RootView()
        }
    }
}

【问题讨论】:

  • 欢迎来到 SO!为了发布问题,您应该携带最少的信息作为输入样本和预期的输出样本(如果需要),您尝试了什么以及您的研究,以显示一些努力,因为 SO 不是免费的编码服务。您尝试和研究了什么?

标签: ios swiftui


【解决方案1】:

可以dispatchQueue延迟吗?

    Button(
        "Here is Detail View. Tap to go back.",
        action: {
            DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
                self.presentationMode.wrappedValue.dismiss()

            }})

【讨论】:

    【解决方案2】:

    这将是一种方法。如果没有NavigationLink,您可以完全控制所有动画和过渡。

    struct DetailView: View {
        @Binding var showDetail:Bool
    
        var body: some View {
            Button(
                "Here is Detail View. Tap to go back.",
                action: {
                    withAnimation(Animation.linear.delay(2)){
                        self.showDetail = false
                    }
                }
            ).frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity).background(Color.yellow)
        }
    }
    
    struct RootView: View {
        @State var showDetail = false
    
        var body: some View {
            VStack {
                if showDetail{
                    DetailView(showDetail:self.$showDetail).transition(.move(edge: .trailing))
                }else{
                    Button("I am Root. Tap for Detail View."){
                        withAnimation(.linear){
                            self.showDetail = true
                        }
                    }
                }
            }.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity).background(Color.red)
        }
    }
    
    

    【讨论】:

    • 我应该在哪里添加rotation3DEffect仅在视图关闭时发生。我想制作一个从右下角到右上角的像泪珠一样的动画
    • 您应该在 DetailView 上定义过渡。 DetailView(showDetail:self.$showDetail).transition(.asymmetric(insertion: .move(edge: .trailing), removal: .scale))。这将允许您为该视图的出现和消失定义两个不同的动画。只需将 .scale 更改为您的自定义动画即可。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多