滚动视图下的 SwiftUI 点击

Posted

技术标签:

【中文标题】滚动视图下的 SwiftUI 点击【英文标题】:SwiftUI tap under scrollview 【发布时间】: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)
        
    

【问题讨论】:

【参考方案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") 
                    
                
            

        
        
        
        
    

【讨论】:

以上是关于滚动视图下的 SwiftUI 点击的主要内容,如果未能解决你的问题,请参考以下文章