【问题标题】:Potential SwiftUI ZStack Transition bug when pressed rapidly快速按下时潜在的 SwiftUI ZStack 转换错误
【发布时间】:2020-05-10 20:49:23
【问题描述】:

我想制作一个可在 iOS 和 Mac 上跨平台运行的模式。

问题是,如果我快速切换模式,就会出现奇怪的行为。附加 GIF 与下面的奇怪行为。这是 SwiftUI 的错误吗?

如果不是,我做错了什么?

编辑:更多信息。 如果没有动画和过渡,交互会按预期进行。

这是 Playground 上的代码

import SwiftUI
import PlaygroundSupport


struct CView: View {
    @State var isShown = false
    var body: some View {
        ZStack {
            Color.white
            ZStack {
                Color.green
                VStack {
                    HStack {
                        Spacer()
                        Text("Open").onTapGesture {
                            self.isShown = true
                        }
                    }
                    Spacer()
                }
            }
                .edgesIgnoringSafeArea(.all)
                .zIndex(0)
            if self.isShown {
                ZStack {
                    Color.red
                    VStack {
                        HStack {
                            Spacer()
                            Text("Close").onTapGesture {
                                self.isShown = false
                            }
                        }
                        Spacer()
                    }
                }
                .transition(AnyTransition.move(edge: .bottom))
                .animation(.easeInOut)
                .edgesIgnoringSafeArea(.all)
                .zIndex(1)

            }
        }
    }
}
// Present the view controller in the Live View window
PlaygroundPage.current.setLiveView(CView())
``


  [1]: https://i.stack.imgur.com/21z7z.gif

【问题讨论】:

    标签: swift xcode swiftui


    【解决方案1】:

    这里有一个解决方案。使用 Xcode 11.5b 测试

    struct CView: View {
        @State private var isShown = false
        @State private var showing = false
        var body: some View {
            ZStack {
                Color.white // needed ??
                ZStack {
                    Color.green
                    VStack {
                        HStack {
                            Spacer()
                            Text("Open").onTapGesture {
                                self.isShown = true
                            }
                        }
                        Spacer()
                    }
                }.disabled(showing)        // << inactive on in-progress
    //                .edgesIgnoringSafeArea(.all) // ?? bad visibility
                .zIndex(1) // by default it is always 0, so make above white
                if self.isShown {
                    ZStack {
                        Color.red
                        VStack {
                            HStack {
                                Spacer()
                                Text("Close").onTapGesture {
                                    self.isShown = false
                                }
                            }
                            Spacer()
                        }
                    }
                    .transition(AnyTransition.move(edge: .bottom))
                    .animation(.easeInOut)
    //                .edgesIgnoringSafeArea(.all)      // ??
                    .zIndex(2)       // top-most
                    .onAppear {
                        self.showing = true
                    }
                    .onDisappear {
                        self.showing = false
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 感谢这项工作。我本来打算做类似的事情,但以前感觉就像是一个黑客。好吧,最好让事情顺利进行!你知道前一个是否是错误/它是预期的行为吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 2021-01-31
    • 2020-08-10
    • 2021-11-18
    • 1970-01-01
    • 2012-12-22
    相关资源
    最近更新 更多