Skip to content

Instantly share code, notes, and snippets.

@Clarko
Last active December 9, 2021 22:20
Show Gist options
  • Save Clarko/8dbe26be36d4d317302791fceda7b30c to your computer and use it in GitHub Desktop.
Save Clarko/8dbe26be36d4d317302791fceda7b30c to your computer and use it in GitHub Desktop.
SwiftUI live-blur materials
// ⚠️ As of WWDC21 you can just use a SwiftUI Material
// https://developer.apple.com/documentation/swiftui/material
//
// Frost.swift
//
// Created by Clarko on 7/19/20.
//
// A SwiftUI representation of UIVisualEffectView
// and UIBlurEffect, that handles mostly like a
// SwiftUI Color view. Use it as a .background()
// or on its own.
//
// Don't try to use it as a .fill() or a .border()
// on a shape, sorry.
//
// Comes with styles like Frost.thin and Frost.thick
//
import SwiftUI
struct Frost: UIViewRepresentable, Hashable {
// Adaptive UI style, matching the system toolbars
static var chrome: Frost {
Frost(style: .systemChromeMaterial)
}
// Adaptive UI styles, by thickness
static var ultraThin: Frost {
Frost(style: .systemUltraThinMaterial)
}
static var thin: Frost {
Frost(style: .systemThinMaterial)
}
static var normal: Frost {
Frost(style: .systemMaterial)
}
static var thick: Frost {
Frost(style: .systemThickMaterial)
}
// Content styles, by brightness
static var lighter: Frost {
Frost(style: .extraLight)
}
static var neutral: Frost {
Frost(style: .light)
}
static var darker: Frost {
Frost(style: .dark)
}
// Implementation
var style: UIBlurEffect.Style = .systemChromeMaterial
func makeUIView(context: Context) -> UIVisualEffectView {
UIVisualEffectView()
}
func updateUIView(_ uiView: UIVisualEffectView, context: Context) {
uiView.effect = UIBlurEffect(style: style)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment