【问题标题】:SwiftUI tap under scrollview滚动视图下的 SwiftUI 点击
【发布时间】:2021-03-06 01:39:48
【问题描述】:

绿色的点击手势不起作用。有人对此有解决方案吗?

ZStack {
    Color.green
        .onTapGesture {
            print("green")
        }
    ScrollView {
        VStack {
            Spacer(minLength: 500)
            Color.red
                .onTapGesture {
                    print("red")
                }
                .frame(height: 800)
        }
    }
}

【问题讨论】:

    标签: swift swiftui scrollview swiftui-scrollview


    【解决方案1】:

    你想要的东西是不可能的,因为 ScrollView 在绿色 View 之上,但是有这样的方法:

        struct ContentView: View {
        
        func greenFunction() { print("green") }
        
        var body: some View {
    
            ZStack {
                
                Color.green.onTapGesture { greenFunction() }
    
                ScrollView {
    
                    VStack(spacing: 0) {
                        
                        Color.white.opacity(0.01).frame(height: 500).onTapGesture { greenFunction() }
                        
                        Color.red.frame(height: 800).onTapGesture { print("red") }
                        
                    }
                }
    
            }
            
            
            
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      相关资源
      最近更新 更多