簡體   English   中英

當視圖位於 ScrollView 內時,onTapGesture 不起作用?

[英]onTapGesture not working when view is inside ScrollView?

我有簡單的看法。

var body: some View {
    NavigationView {
        ZStack {
            ScrollView {
                GeometryReader { geometry in
                    CardView(geometry: geometry)
                    .onTapGesture {
                            print("Tapped")
                        }
                }
            .padding()
            }
        }
    }

當我點擊卡片時,什么都沒有打印出來。 但是,例如,如果我將scrollView更改為VStack ,我會Tapped在控制台上被點擊。 怎么了? 如何在滾動視圖內的卡片上實現點擊手勢?

您的問題可能出在 GeometryReader 上,請嘗試將其移至 scrollView 上方而不是內部

var body: some View {
        NavigationView {
            ZStack {
                Color("light_blue_grey")
                    .edgesIgnoringSafeArea(.all)
                GeometryReader { geometry in
                    ScrollView {
                        Rectangle()
                            .foregroundColor(.blue)
                            .frame(width: geometry.size.width, height: 100)
                            .onTapGesture {
                                print("Tapped!")
                        }
                        
                    }
                    .padding()
                }
            }
        }
    }

暫無
暫無

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

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