Skip to content

Instantly share code, notes, and snippets.

@b3ll
Created November 10, 2019 07:15
Show Gist options
  • Save b3ll/ca222a53eef1f4b1319fd02a014718ef to your computer and use it in GitHub Desktop.
Save b3ll/ca222a53eef1f4b1319fd02a014718ef to your computer and use it in GitHub Desktop.
iOS SwiftUI Blurry Vibrant Container View
//
// BlurryVibrantView.swift
//
//
// Created by Adam Bell on 11/9/19.
// Copyright © 2019 Adam Bell. All rights reserved.
//
import SwiftUI
import UIKit
class BlurryVibrantView_<Content: View>: UIView {
let blurView: UIVisualEffectView
let vibrancyView: UIVisualEffectView
var blurEffectStyle: UIBlurEffect.Style = .regular {
didSet {
if oldValue == blurEffectStyle {
return
}
updateBlurStyle()
updateVibrancyStyle()
}
}
var vibrancyEffectStyle: UIVibrancyEffectStyle = .label {
didSet {
if oldValue == vibrancyEffectStyle {
return
}
updateVibrancyStyle()
}
}
var hostingView: UIView? {
didSet {
oldValue?.removeFromSuperview()
if let hostingView = hostingView {
self.vibrancyView.contentView.addSubview(hostingView)
}
setNeedsLayout()
}
}
var hostingController: UIHostingController<Content>!
override init(frame: CGRect) {
self.blurView = UIVisualEffectView(effect: nil)
self.vibrancyView = UIVisualEffectView(effect: nil)
super.init(frame: frame)
addSubview(blurView)
blurView.contentView.addSubview(vibrancyView)
updateBlurStyle()
updateVibrancyStyle()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
let bounds = self.bounds
blurView.frame = bounds
vibrancyView.frame = bounds
hostingView?.frame = bounds
}
fileprivate func updateBlurStyle() {
blurView.effect = UIBlurEffect(style: blurEffectStyle)
}
fileprivate func updateVibrancyStyle() {
guard let blurEffect = blurView.effect as? UIBlurEffect else { return }
vibrancyView.effect = UIVibrancyEffect(blurEffect: blurEffect, style: vibrancyEffectStyle)
}
}
struct BlurryVibrantView<Content: View>: UIViewRepresentable {
typealias UIViewType = BlurryVibrantView_<Content>
private let rootView: Content
private let blurEffectStyle: UIBlurEffect.Style
private let vibrancyEffectStyle: UIVibrancyEffectStyle
init(blurEffectStyle: UIBlurEffect.Style, vibrancyEffectStyle: UIVibrancyEffectStyle, @ViewBuilder content: () -> Content) {
self.rootView = content()
self.blurEffectStyle = blurEffectStyle
self.vibrancyEffectStyle = vibrancyEffectStyle
}
func makeUIView(context: Context) -> UIViewType {
let view = BlurryVibrantView_<Content>(frame: .zero)
view.blurEffectStyle = blurEffectStyle
view.vibrancyEffectStyle = vibrancyEffectStyle
view.hostingController = UIHostingController(rootView: rootView)
view.hostingView = view.hostingController.view
return view
}
func updateUIView(_ uiView: UIViewType, context: Context) {
uiView.blurEffectStyle = blurEffectStyle
uiView.vibrancyEffectStyle = vibrancyEffectStyle
uiView.hostingController.rootView = rootView
uiView.hostingController.view.setNeedsDisplay()
}
}
@Bharat-34
Copy link

can expect explination ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment