Created
September 9, 2024 11:29
-
-
Save wotjd/a7ac101dabcc08d3f7bb76d6bf736fc1 to your computer and use it in GitHub Desktop.
implementation of figma's background blur
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct VisualEffectView: UIViewRepresentable { | |
let visualEffectViewConfiguration: (UIVisualEffectView) -> Void | |
init( | |
visualEffectViewConfiguration: @escaping (UIVisualEffectView) -> Void = { _ in } | |
) { | |
self.visualEffectViewConfiguration = visualEffectViewConfiguration | |
} | |
public func makeUIView(context: Context) -> UIVisualEffectView { | |
let visualEffectView = UIVisualEffectView() | |
visualEffectViewConfiguration(visualEffectView) | |
return visualEffectView | |
} | |
func updateUIView(_ uiView: UIVisualEffectView, context: Context) { } | |
} | |
struct BackgroundBlurView: View { | |
let radius: CGFloat | |
let tintColor: Color? | |
init(radius: CGFloat, tintColor: Color? = nil) { | |
self.radius = radius | |
self.tintColor = tintColor | |
} | |
var body: some View { | |
VisualEffectView { | |
$0.effect = UIBlurEffect() | |
$0.backgroundColor = tintColor.map(UIColor.init(_:)) | |
} | |
// NOTE: don't know why but it looks like | |
.blur(radius: radius / 2, opaque: true) | |
} | |
} | |
#Preview { | |
Image(systemName: "dog") | |
.resizable() | |
.scaledToFit() | |
.frame(width: 100, height: 100) | |
.overlay { | |
BackgroundBlurView(radius: 20) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment