Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of .compositingGroup() in SwiftUI?

Tags:

swiftui

I cannot figure out what compositingGroup() is. At first, I thought it is something like Merging layers in Photoshop. But it was not. Because .shadow() effects to the overlay and background views respectively even if I use .compositingGroup().

So far, I've found 2 differences when I use .compositingGroup()

  • Text doesn't have shadows.
  • The shadow size of the overlay view is slightly smaller than the above one.

What is the purpose of compositingGroup?

struct ContentView: View {
    var body: some View {
        VStack(spacing: 50) {
            Text("Without\ncompositing")
                .font(.largeTitle)
                .bold()
                .padding()
                .foregroundColor(Color.white)
                .background(RoundedRectangle(cornerRadius: 30).fill(Color.red))
                .padding()
                .padding()
                .overlay(RoundedRectangle(cornerRadius: 30).stroke(lineWidth: 10))
                .shadow(color: .blue, radius: 5)

            Text("With\ncompositing")
                .font(.largeTitle)
                .bold()
                .padding()
                .foregroundColor(Color.white)
                .background(RoundedRectangle(cornerRadius: 30).fill(Color.red))
                .padding()
                .padding()
                .overlay(RoundedRectangle(cornerRadius: 30).stroke(lineWidth: 10))
                .compositingGroup() // <--- I added .compositingGroup() here.
                .shadow(color: .blue, radius: 5)
        }
    }
}

enter image description here

like image 836
user2848557 Avatar asked Apr 29 '20 11:04

user2848557


2 Answers

This modifier makes the following modifiers be applied to the view as a whole and not to each particular subview separately

Here's an example to better illustrate this:

struct ContentView: View {
    let circles: some View = ZStack {
        Circle()
            .frame(width: 100, height: 100)
            .foregroundColor(.red)
            .offset(y: -25)

        Circle()
            .frame(width: 100, height: 100)
            .foregroundColor(.blue)
            .offset(x: -25, y: 25)

        Circle()
            .frame(width: 100, height: 100)
            .foregroundColor(.green)
            .offset(x: 25, y: 25)
    }

    var body: some View {
        VStack(spacing: 100) {
            circles

            circles
                .opacity(0.5)

            circles
                .compositingGroup()
                .opacity(0.5)
        }
    }
}

result

So in your case the shadow is applied to the whole view rather than separately to the Text and overlaying RoundedRectangle

like image 173
ramzesenok Avatar answered Oct 24 '22 06:10

ramzesenok


Use it when wanting to apply effects like opacity or shadow to a group of views and not each contained element by itself.

like image 35
Anton Klymenko Avatar answered Oct 24 '22 07:10

Anton Klymenko