【问题标题】:SwiftUI bug or code issue with onTapGesture combine with other View?onTapGesture 与其他视图结合的 SwiftUI 错误或代码问题?
【发布时间】:2021-01-14 01:02:11
【问题描述】:

我希望 SwiftUI 可以了解我在哪里点击我的应用程序,因此我为我的视图提供了 .onTapGesture 可能性,在此代码中,视图大于其父视图,但我可以使用剪辑解决问题,问题得到解决视觉上,但在代码 SwiftUI 中看到原始大小的 Rec,我想知道这种错误行为是否是 SwiftUI 中的错误,或者我们用一些魔术代码解决它的代码?谢谢大家。

gif:

struct ContentView: View {
var body: some View {
    
    ZStack {
        
        Color.white.ignoresSafeArea()
            .onTapGesture { print("you tapped on Screen!") }
        
        
        Circle()
            .fill(Color.red)
            .frame(width: 300, height: 300, alignment: .center)
            .overlay(
                
                Rectangle()
                    .fill(Color.yellow)
                    .frame(width: 100, height: UIScreen.main.bounds.height, alignment: .center)
                    .onTapGesture { print("you tapped on Rectangle!") }
                
            )
            .clipShape(Circle())
            .onTapGesture { print("you tapped on Circle!") }
        
    }
    
    
    }
}

【问题讨论】:

    标签: swiftui


    【解决方案1】:

    只需在 onTapGesture 之前使用contentShape(Circle()) 即可修复

    Circle()
        .fill(Color.red)
        .frame(width: 300, height: 300, alignment: .center)
        .overlay(                
            Rectangle()
                .fill(Color.yellow)
                .frame(width: 100, height: UIScreen.main.bounds.height, alignment: .center)
                .onTapGesture { print("you tapped on Rectangle!") }
        )
        .clipShape(Circle())
        .contentShape(Circle()) //<< contentShape here 
        .onTapGesture { print("you tapped on Circle!") }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-27
      • 2021-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多