简体   繁体   English

为什么 SwiftUI 没有注册我在圆角矩形背景上的点击?

[英]Why SwiftUI does not register my tap on Background of RoundedRectangle?

I wanted to try this down code to see if SwiftUI understand between RoundedRectangle and Rectangle, therefore I ran this code, SwiftUI could tell me the difference between Screen Background tap and Rectangle tap, but it is unable to understand between RoundedRectangle itself and Background of RoundedRectangle which is a Rectangle.我想试试这个向下的代码,看看 SwiftUI 是否理解 RoundedRectangle 和 Rectangle,因此我运行了这个代码,SwiftUI 可以告诉我屏幕背景点击和矩形点击之间的区别,但它无法理解 RoundedRectangle 本身和 RoundedRectangle 的背景这是一个矩形。

How could I solve this issue?我该如何解决这个问题?

Goal: I want get print for white area on tap, as well I am getting for red and yellow area.目标:我想在水龙头上打印白色区域,我也想打印红色和黄色区域。

struct ContentView: View {
    var body: some View {

        ZStack {
            
            Color.red
                .ignoresSafeArea()
                .onTapGesture{ print("you tapped on Screen Background! 🟥") }

            RoundedRectangle(cornerRadius: 90)
                .fill(Color.yellow)
                .frame(width: 300, height: 300, alignment: .center)
                .background(Color.white.onTapGesture{ print("you tapped on RoundedRectangle Background! ⬜️") })
                .onTapGesture{ print("you tapped on RoundedRectangle! 🟨") }

        }
    }
}

在此处输入图像描述

This looks like a bug indeed.这看起来确实像一个错误。

As a workaround you can use a custom shape, eg RoundedCorner taken from here :作为一种解决方法,您可以使用自定义形状,例如取自此处RoundedCorner

struct RoundedCorner: Shape {
    var radius: CGFloat = .infinity
    var corners: UIRectCorner = .allCorners

    func path(in rect: CGRect) -> Path {
        let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        return Path(path.cgPath)
    }
}

Then, it will work as expected:然后,它将按预期工作:

struct ContentView: View {
    var body: some View {
        ZStack {
            Color.red
                .ignoresSafeArea()
                .onTapGesture { print("you tapped on Screen Background! 🟥") }
            Color.white
                .frame(width: 300, height: 300, alignment: .center)
                .onTapGesture { print("you tapped on RoundedRectangle Background! ⬜️") }
            RoundedCorner(radius: 90)
                .fill(Color.yellow)
                .frame(width: 300, height: 300, alignment: .center)
                .onTapGesture { print("you tapped on RoundedRectangle! 🟨") }
        }
    }
}

or, with Color.white as background :或者,以Color.white作为background

struct ContentView: View {
    var body: some View {
        ZStack {
            Color.red
                .ignoresSafeArea()
                .onTapGesture { print("you tapped on Screen Background! 🟥") }

            RoundedCorner(radius: 90)
                .fill(Color.yellow)
                .frame(width: 300, height: 300, alignment: .center)
                .onTapGesture { print("you tapped on RoundedRectangle! 🟨") }
                .background(
                    Color.white
                        .onTapGesture { print("you tapped on RoundedRectangle Background! ⬜️") }
                )
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM