SwiftUI onTapGesture 只调用一次

SwiftUI onTapGesture called only once

我正在研究这个带有多个视图组件的音频播放器。 当我们在中间视图的任意位置单击时,我添加了一种方法来 hide/show 顶部视图和底部视图。

之前它工作正常,但最近当我再次尝试时,它只是关闭它并且不会再次触发 onTapGesture

我认为与之前的唯一区别是视图是 presented 而不是视图控制器中的 pushed

我尝试在 onEnded() 上使用带有 TapGesture() 的自定义手势,但结果相同。 我还尝试添加一个矩形,如 [此处][1] 所述。

struct PlayerView: View {
    @ObservedObject private var playerState = PlayerState()
    @Binding var isPlayerReduced: Bool

    private let interfaceColor: Color = .gray//.black
    private let interfaceOpacity: Double = 0.9
    private let interfaceAnimationDuration: Double = 0.4

    var body: some View {
        ZStack(content: {
            GeometryReader(content: { geometry in
                VStack(content: {
                    if !self.playerState.isInterfaceHidden {
                        TopPlayerView(playerState: self.playerState,
                                      isPlayerReduced: self.$isPlayerReduced)
                            .transition(.opacity)
                            .background(self.interfaceColor.opacity(self.interfaceOpacity))
                    }
                    MiddlePlayerView(skipIntro: self.$playerState.skipIntro)
                        // Allow to spread the background zone for click purposes
                        .background(Color.clear)
                        // I want to have the middle under my TopPlayer and my BottomPlayer
                        .zIndex(-1)
                        .onTapGesture(perform: {
                            withAnimation(.easeInOut(duration: self.interfaceAnimationDuration)) {
                                self.playerState.isInterfaceHidden.toggle()
                            }
                        })
                    //                            .gesture(TapGesture()
                    //                                .onEnded({ _ in
                    //                                }))
                    if !self.playerState.isInterfaceHidden {
                        BottomPlayerView(playerState: self.playerState)
                            .padding(.bottom, geometry.safeAreaInsets.bottom)
                            .transition(.opacity)
                            .background(self.interfaceColor.opacity(self.interfaceOpacity))
                    }
                })
            })
        })
            .background(Color.black)
            .edgesIgnoringSafeArea(.all)
            .navigationBarTitle("")
            .navigationBarHidden(true)
    }
}

我在这里有点想法,欢迎任何帮助!谢谢!

好的,所以在接触了这段代码中的所有可能之后。我最终让它工作了。 不同之处在于我将填充放在我的视图中。 我将填充切换到 VStack 而不是我在 VStack 中的视图。 它现在似乎可以工作了。

我post下面的工作代码。

var body: some View {
        ZStack(alignment: .center, content: {
            GeometryReader(content: { geometry in
                VStack(content: {
                    self.topMarker()
                    if !self.playerState.isInterfaceHidden {
                        TopPlayerView(playerState: self.playerState,
                                      isPlayerReduced: self.$isPlayerReduced)
                            .transition(.opacity)
                            .background(self.interfaceColor.opacity(self.interfaceOpacity))
                    }
                    MiddlePlayerView(skipIntro: self.$playerState.skipIntro)
                        // Allow to spread the background zone for click purposes
                        .background(Color.white.opacity(0.00000001))
                        // I want to have the middle under my TopPlayer and my BottomPlayer
                        .zIndex(-1)
                        .onTapGesture(perform: {
                            withAnimation(.easeInOut(duration: self.interfaceAnimationDuration)) {
                                self.playerState.isInterfaceHidden.toggle()
                            }
                        })
                    if !self.playerState.isInterfaceHidden {
                        BottomPlayerView(playerState: self.playerState)
                            .transition(.opacity)
                            .background(self.interfaceColor.opacity(self.interfaceOpacity))
                    }
                })
                    .padding(.top, 8)
                    .padding(.bottom, geometry.safeAreaInsets.bottom)
            })
        })
            .background(Color.black)
            .edgesIgnoringSafeArea(.all)
            .navigationBarTitle("")
            .navigationBarHidden(true)
    }

老实说,我不知道这里有什么不同... 即使在视图调试器中也没有区别..