Last active
September 11, 2024 04:34
-
-
Save Rukh/0eeedcb99fe057d1dba00d426c3fa105 to your computer and use it in GitHub Desktop.
UIVisualEffectView with any blur radius in SwiftUI
This file contains 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
// | |
// 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