简体   繁体   中英

How to apply a blur to shadow in SwiftUI

So I am building a custom component and the figma file has the following parameters.

Rectangle Background Drop Shadow 1: Offset X = 0px, Y = 4px
Rectangle Background Drop Shadow 1: Blur = 8px, Spread = -1px
Rectangle Background Drop Shadow Color 1: 16% #000000

I am having a hard time figuring out how to add the blur and the spread. This is what I have so far.

I have the drop shadow working, but it is harsh without the blur and spread

.background(Color.newPrimary
                            .shadow(color: .black, radius: 6, x: 0, y: 4)
                            )

Try adding a second background. You can't blur a shadow directly. Blur will act on the whole view, as I am sure you found out. If you don't want to blur your background, add another background of the shadow color, add the shadow and then blur. I through an .opacity() modifier in there as well to further tweak the look.

    Text("Hello, World!")
        .padding()
        .background(Color.red)
        .background(Color.black
                        .opacity(0.5)
                        .shadow(color: .black, radius: 6, x: 0, y: 4)
                        .blur(radius: 8, opaque: false)
        )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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