nikstar / VariableBlur

SwiftUI variable blur (progressive blur)
MIT License
69 stars 0 forks source link

Is there a way to create a darker blur? #5

Open Pranoy1c opened 1 month ago

Pranoy1c commented 1 month ago

Is there a way to create a darker blur? Dark enough so that the status bar always shows white text instead of black? How can I control?

nikstar commented 1 month ago

Currently, no. As a workaround overlay Color.black.opacity(...) to Color.clear gradient.

In fact I disable darkening and saturation layers of UIVisualEffectView because it creates a visible line at clear end of the view:

https://github.com/nikstar/VariableBlur/blob/41698eecb3373550b2100dbcd308d886501d7033/Sources/VariableBlur/VariableBlur.swift#L69-L72

Ideally, we wouldn't do this and instead apply opacity gradient.

Also change style used to explicitly dark material:

https://github.com/nikstar/VariableBlur/blob/41698eecb3373550b2100dbcd308d886501d7033/Sources/VariableBlur/VariableBlur.swift#L42

Any PRs that tackle this will be absolutely welcome!

Pranoy1c commented 1 month ago

I was able to create darkened gradient blur using the solution by @Sweeper here:

https://stackoverflow.com/a/78877606/1634905

In the VariableBlurUIView, add:

let darkLayer = CALayer()

Then, in the init, add:

darkLayer.contents = gradientImage
darkLayer.zPosition = -1
backdropLayer?.superLayer?.addSublayer(darkLayer)

Add the following functions:

open override func layoutSublayers(of layer: CALayer) {
    darkLayer.frame = subviews.first?.layer.frame ?? .zero
}

And change the color0 in makeGradientImage func to black with alpha which you think is appropriate:

CIColor(color: UIColor.black.withAlphaComponent(0.7))

Result:

Screenshot 2024-08-17 at 2 29 08 PM

nikstar commented 1 month ago

Oh, you use makeGradientImage. Nice. Note that variableBlur uses opacity to determine blur radius so if you reuse the same clear to 0.7 black gradient, variable blur will only go from 0 to 0.7*maxBlurRadius.