Skip to content

Instantly share code, notes, and snippets.

@EthanLipnik
Last active March 6, 2024 04:00
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EthanLipnik/6832059aea4cbdce7fb17b2b05eb8ef4 to your computer and use it in GitHub Desktop.
Save EthanLipnik/6832059aea4cbdce7fb17b2b05eb8ef4 to your computer and use it in GitHub Desktop.
Blur transition for SwiftUI
//
// Blur.swift
//
//
// Created by Ethan Lipnik on 11/21/22.
//
import SwiftUI
private struct BlurModifier: ViewModifier {
public let isIdentity: Bool
public var intensity: CGFloat
public func body(content: Content) -> some View {
content
.blur(radius: isIdentity ? intensity : 0)
.opacity(isIdentity ? 0 : 1)
}
}
public extension AnyTransition {
static var blur: AnyTransition {
.blur()
}
static var blurWithoutScale: AnyTransition {
.modifier(
active: BlurModifier(isIdentity: true, intensity: 5),
identity: BlurModifier(isIdentity: false, intensity: 5)
)
}
static func blur(
intensity: CGFloat = 5,
scale: CGFloat = 0.8,
scaleAnimation animation: Animation = .spring()
) -> AnyTransition {
.scale(scale: scale)
.animation(animation)
.combined(
with: .modifier(
active: BlurModifier(isIdentity: true, intensity: intensity),
identity: BlurModifier(isIdentity: false, intensity: intensity)
)
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment