Skip to content

Instantly share code, notes, and snippets.

@Rukh
Last active January 3, 2024 07:05
Show Gist options
  • Save Rukh/0eeedcb99fe057d1dba00d426c3fa105 to your computer and use it in GitHub Desktop.
Save Rukh/0eeedcb99fe057d1dba00d426c3fa105 to your computer and use it in GitHub Desktop.
UIVisualEffectView with any blur radius in SwiftUI
//
// BackdropBlurView.swift
//
// Created by Dmitry Gulyagin on 20.09.2022.
//
import SwiftUI
/// A View which content reflects all behind it
struct BackdropView: UIViewRepresentable {
public func makeUIView(context: Context) -> UIVisualEffectView {
let view = UIVisualEffectView()
let blur = UIBlurEffect()
let animator = UIViewPropertyAnimator()
animator.addAnimations { view.effect = blur }
animator.fractionComplete = 0
animator.stopAnimation(false)
animator.finishAnimation(at: .current)
return view
}
func updateUIView(_ uiView: UIVisualEffectView, context: Context) { }
}
/// A transparent View that blurs its background
struct BackdropBlurView: View {
let radius: CGFloat
@ViewBuilder
var body: some View {
BackdropView().blur(radius: radius, opaque: true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment