Skip to content

Commit f5d3e8e

Browse files
committed
Update VisualEffectView.swift
1 parent 5784874 commit f5d3e8e

1 file changed

Lines changed: 35 additions & 11 deletions

File tree

Sources/VisualEffectView/VisualEffectView.swift

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,15 @@ open class VisualEffectView: UIVisualEffectView {
6565
}
6666

6767
/// High-level switch for the backing material.
68-
/// Default keeps existing behavior by using `.customBlur`.
68+
/// Default is `.customBlur` for backward compatibility.
69+
///
70+
/// - Note: When using `.systemBlur` or `.glass`, custom blur properties
71+
/// (blurRadius, colorTint, etc.) are ignored.
6972
public var style: VisualEffectStyle = .customBlur {
70-
didSet { applyStyle(style) }
73+
didSet {
74+
guard style != oldValue else { return }
75+
applyStyle(style)
76+
}
7177
}
7278

7379
// MARK: - Preserve custom settings across style switches
@@ -100,6 +106,8 @@ open class VisualEffectView: UIVisualEffectView {
100106
}
101107
set {
102108
customSnapshot.colorTint = newValue
109+
guard case .customBlur = style else { return }
110+
103111
prepareForChanges()
104112
sourceOver?.setValue(newValue, forKeyPath: "color")
105113
sourceOver?.perform(Selector(("applyRequestedEffectToView:")), with: overlayView)
@@ -132,6 +140,8 @@ open class VisualEffectView: UIVisualEffectView {
132140
}
133141
set {
134142
customSnapshot.blurRadius = newValue
143+
guard case .customBlur = style else { return }
144+
135145
prepareForChanges()
136146
gaussianBlur?.requestedValues?["inputRadius"] = newValue
137147
applyChanges()
@@ -150,6 +160,8 @@ open class VisualEffectView: UIVisualEffectView {
150160
get { return _value(forKey: .saturationDeltaFactor) ?? 1.0 }
151161
set {
152162
customSnapshot.saturation = newValue
163+
guard case .customBlur = style else { return }
164+
153165
_setValue(newValue, forKey: .saturationDeltaFactor)
154166
}
155167
}
@@ -166,21 +178,33 @@ open class VisualEffectView: UIVisualEffectView {
166178
get { return _value(forKey: .scale) ?? 1.0 }
167179
set {
168180
customSnapshot.scale = newValue
181+
guard case .customBlur = style else { return }
182+
169183
_setValue(newValue, forKey: .scale)
170184
}
171185
}
172186

173187
// MARK: - Initialization
174188

189+
/// Creates a visual effect view with customizable blur (legacy default for backward compatibility).
190+
public convenience init() {
191+
self.init(effect: nil)
192+
}
193+
175194
public override init(effect: UIVisualEffect?) {
176195
super.init(effect: effect)
177-
// Keep previous default behavior: custom pipeline “on”
178-
applyStyle(style)
196+
197+
// If no effect provided, use legacy default for backward compatibility
198+
if effect == nil {
199+
applyStyle(.customBlur)
200+
}
201+
// Otherwise, respect the passed effect
179202
}
180203

181204
required public init?(coder aDecoder: NSCoder) {
182205
super.init(coder: aDecoder)
183-
applyStyle(style)
206+
// Interface Builder instances default to custom blur
207+
applyStyle(.customBlur)
184208
}
185209

186210
}
@@ -198,30 +222,29 @@ private extension VisualEffectView {
198222
self.effect = UIBlurEffect(style: style)
199223

200224
case .customBlur:
201-
// Switch back to your private/custom effect object
225+
// Switch back to custom effect object
202226
self.effect = blurEffect
203227
// Re-apply settings snapshot so switching styles is reversible
204228
reapplyCustomSnapshot()
205229

206230
case .glass(let glass):
207231
if #available(iOS 26.0, *) {
208-
self.effect = UIGlassEffect(style: glass.uiStyle) // UIKit iOS 26 API
232+
self.effect = UIGlassEffect(style: glass.uiStyle)
209233
} else {
210-
// graceful fallback on older OS
211-
self.effect = UIBlurEffect(style: .systemThinMaterial)
234+
// Graceful fallback on older OS with style-appropriate blur
235+
self.effect = UIBlurEffect(style: glass.fallbackBlurStyle)
212236
}
213237
}
214238
}
215239

216240
func reapplyCustomSnapshot() {
217-
// Apply in a safe order; these call into your existing private pipeline
241+
// Apply in a safe order; these call into the existing custom pipeline
218242
self.scale = customSnapshot.scale
219243
self.saturation = customSnapshot.saturation
220244
self.blurRadius = customSnapshot.blurRadius
221245
self.colorTint = customSnapshot.colorTint
222246
}
223247

224-
225248
}
226249

227250
// MARK: - Helpers
@@ -244,4 +267,5 @@ private extension VisualEffectView {
244267

245268
}
246269

270+
// Available keys for reference:
247271
// ["grayscaleTintLevel", "grayscaleTintAlpha", "lightenGrayscaleWithSourceOver", "colorTint", "colorTintAlpha", "colorBurnTintLevel", "colorBurnTintAlpha", "darkeningTintAlpha", "darkeningTintHue", "darkeningTintSaturation", "darkenWithSourceOver", "blurRadius", "saturationDeltaFactor", "scale", "zoom"]

0 commit comments

Comments
 (0)