简体   繁体   English

SwiftUI 屏蔽圆角矩形内的矩形

[英]SwiftUI Mask a rectangle inside a rounded rectangle

卡片

Hello there.你好呀。 I am wondering, in SwiftUI, how do you mask the contents of a rounded rectangle so that a child rectangle clips the corners.我想知道,在 SwiftUI 中,如何屏蔽圆角矩形的内容,以便子矩形剪辑角。

In my example I have a white rounded rectangle and a pink rectangle on a zstack, I've tried to apply clipping, but the pink rectangle does not conform to the corners.在我的示例中,我在 zstack 上有一个白色圆角矩形和一个粉红色矩形,我尝试应用剪辑,但粉红色矩形不符合角。

I've tried applying.mask to the white rectangle, but it gives different results to expectations (sometimes it doesn't show the pink rectangle).我已经尝试将 apply.mask 应用于白色矩形,但它给出了与预期不同的结果(有时它不显示粉红色矩形)。

I did find an example where you can set your own cornerRadius Round Specific Corners SwiftUI我确实找到了一个示例,您可以在其中设置自己的cornerRadius Round Specific Corners SwiftUI

But I was wondering if perhaps there was a way to mask the internals/body of the pink rectangle so that it conforms to the parent's rounded rectangle?但我想知道是否有办法掩盖粉红色矩形的内部/主体,使其符合父级的圆角矩形?

My code follows;我的代码如下;

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)
    }

Edit: To clarify:编辑:澄清:

The pink rectangle should remain as a rectangle, but clip the top left and right to match the parent white rounded rectangle.粉红色矩形应保持为矩形,但裁剪左上角和右上角以匹配父白色圆角矩形。

If I correctly understood your goal, here is a solution (tested with Xcode 11.4)如果我正确理解了您的目标,这是一个解决方案(使用 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)

@Asperi already posted a great answer, I have done this aswell with using mask modifier in SwiftUI . @Asperi 已经发布了一个很好的答案,我也通过在SwiftUI中使用mask修饰符来做到这一点。 Furthermore you only have to set cornerRadius once.此外,您只需设置一次cornerRadius

VStack(spacing: 0)
{
    ZStack(alignment: .center)
    {
       Rectangle()
       .fill(Color.red)
       .frame(width: 66, height: 20)

    }


    ZStack(alignment: .center)
    {
       Rectangle()
       .fill(Color.white)
       .frame(width: 66, height: 46)

    }
}
.mask(Rectangle()
        .cornerRadius(3.0)
        .frame(width: 66, height: 66)
)

在此处输入图像描述

在此处输入图像描述 check this out看一下这个

struct ContentView: View {
    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)
                            .clipShape(RoundedRectangle(cornerRadius: 16)) // <<<<<<


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

        }
        .edgesIgnoringSafeArea(.all)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

I think the easier way is to apply cornerradius to ZStack我认为更简单的方法是将cornerradius应用于ZStack

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() //<<= here
}
.frame(width: 300, height: 450, alignment: .center)
.cornerRadius(20) //<<= here

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

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