SwiftUI Transparency and Mask - How to Use Mask to Clip the Content with Opacity and Gradient

In this tutorial, you will learn how to use transparency and masks in SwiftUI to create stunning visual effects. You'll explore how to adjust the opacity of views, apply gradient colors, and use masks to clip the content of views to specific shapes. These techniques will enable you to create unique and dynamic user interfaces that make your apps stand out. 

You will gain an understanding of how to manipulate the transparency of individual views and the background, apply gradient colors, and work with masks to create complex shapes. You will also learn how to combine these techniques to create dynamic layouts that respond to user interactions.

The mask modifier is even more versatile than the clipShape because you can use colors and even opacity to clip your content.

How to use mask to clip the content with opacity and gradient 

Transparency Mask  

Clipping is especially great for masking content like text and images. 

In this example, both the content and the background will be at 30% opacity.

VStack {
	Text("Transparency Mask")
		.font(.title3).bold()
}
.padding()
.background(Color.blue)
.mask(Color.black.opacity(0.3))

..

Alpha Gradient Mask  

  • A great strategy for dealing with content that gets clipped abrutly is to apply an opaque gradient mask. 
  • In this example, we're masking the content using a gradient from top at 100% opacity, to bottom at 0% opacity.

.mask(
    LinearGradient(gradient: Gradient(colors: [Color.black, Color.black.opacity(0)]), startPoint: .top, endPoint: .bottom)
)

Final Output:

In this sample layout, we're apply a gradient mask to a clipped content that beautifully adapts to the backgound. 

 ZStack {
                    LinearGradient(gradient: Gradient(colors: [Color(#colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.8549019694, alpha: 1)), Color(#colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)),Color(#colorLiteral(red: 0.3647058904, green: 0.06666667014, blue: 0.9686274529, alpha: 1))]), startPoint: .topLeading, endPoint: .bottomTrailing)
                        .ignoresSafeArea()

            VStack(alignment: .center, spacing: 10.0) {
                        Text("SWIFT UI IO")
                            .kerning(3)
                            .font(.title).bold()
                            .foregroundColor(.black)
                            .shadow(radius: 20)
                        
                        Text("A complete series of tutorials covering Xcode, and SwiftUI")
                            .kerning(1)
                            .font(.title)
                            .foregroundColor(.black)
                            .shadow(radius: 20)
                        
                    }
                    .frame(height: 180, alignment: .center)
                    .padding()
                    .background(Color.white)
                    .mask(
                        LinearGradient(gradient: Gradient(colors: [Color.black, Color.black, Color.black, Color.black.opacity(0)]), startPoint: .top, endPoint: .bottom)
                    )
                    .cornerRadius(30)
                    .padding()
                }

..

..

Comments

  1. Your SwiftUI tutorial on transparency and masks is a total eye-opener! Thanks for sharing the magic!

    ReplyDelete

Post a Comment