SwiftUI 遮罩圆角矩形内的矩形

zar*_*don 4 mask ios round-rect swiftui

卡片

你好呀。我想知道,在 SwiftUI 中,如何屏蔽圆角矩形的内容,以便子矩形剪辑角。

在我的示例中,我在 zstack 上有一个白色圆角矩形和一个粉红色矩形,我尝试应用裁剪,但粉红色矩形不符合边角。

我试过将 .mask 应用于白色矩形,但它给出了与预期不同的结果(有时它不显示粉红色矩形)。

我确实找到了一个示例,您可以在其中设置自己的cornerRadius Round Specific Corners SwiftUI

但我想知道是否有办法掩盖粉红色矩形的内部/主体,使其符合父级的圆角矩形?

我的代码如下;

var body: some View {
        GeometryReader { geometry in

            Color.gray
                .edgesIgnoringSafeArea(.top)
                .overlay(

                    ZStack (alignment: .topLeading) {

                        RoundedRectangle(cornerRadius: 16,
                                         style: .continuous)
                            .foregroundColor(.white)
                            .shadow(radius: 10)
                             // Tried using .mask here 

                        Rectangle()
                            .fill(Color.pink)
                            .frame(minWidth: 0, maxWidth: .infinity, maxHeight: 150, alignment: .top)
                            .clipped()


                    }
                    .frame(width: 300, height: 450, alignment: .center)
            )

        }
        .edgesIgnoringSafeArea(.all)
    }
Run Code Online (Sandbox Code Playgroud)

编辑:澄清:

粉红色矩形应保持为矩形,但剪裁左上方和右上方以匹配父白色圆角矩形。

Asp*_*eri 8

如果我正确理解您的目标,这里是一个解决方案(使用 Xcode 11.4 测试)

演示

ZStack (alignment: .topLeading) {

    RoundedRectangle(cornerRadius: 16,
                     style: .continuous)
        .foregroundColor(.white)
        .shadow(radius: 10)
         // Tried using .mask here

    Rectangle()
        .fill(Color.pink)
        .frame(minWidth: 0, maxWidth: .infinity, maxHeight: 150, alignment: .top)
        .clipped()


}
.clipShape(RoundedRectangle(cornerRadius: 16))       // << here !!
.frame(width: 300, height: 450, alignment: .center)
Run Code Online (Sandbox Code Playgroud)