簡體   English   中英

SwiftUI onTapGesture 僅調用一次

[英]SwiftUI onTapGesture called only once

我正在開發這個帶有多個視圖組件的音頻播放器。 當我們單擊中間視圖中的任意位置時,我添加了一種隱藏/顯示頂視圖和底視圖的方法。

在它工作正常之前,但最近當我再次嘗試時,它只會關閉它並且不會再次觸發onTapGesture

我相信與之前的唯一區別是視圖是呈現而不是推送到視圖 controller 中。

我嘗試在TapGesture() onEnded()自定義手勢,但結果相同。 我還嘗試添加一個 Rectangle 形狀,如 [here][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中的視圖。 它現在似乎起作用了。

我在工作代碼下面發布。

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)
    }

老實說,我不知道這里有什么區別......即使在視圖調試器中也沒有區別......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM