Skip to content

Commit

Permalink
Fix visible pixelization at unblurred edge (#1)
Browse files Browse the repository at this point in the history
Change `startOffset` to zero
  • Loading branch information
nikstar committed Nov 27, 2023
1 parent c3568c6 commit 7b64063
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions Sources/VariableBlur/VariableBlur.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ public enum VariableBlurDirection {
public struct VariableBlurView: UIViewRepresentable {

public var maxBlurRadius: CGFloat = 20
public var startOffset: CGFloat = -0.1

public var direction: VariableBlurDirection = .blurredTopClearBottom

/// By default, variable blur starts from 0 blur radius and linearly increases to `maxBlurRadius`. Setting `startOffset` to a small negative coefficient (e.g. -0.1) will start blur from larger radius value which might look better in some cases.
public var startOffset: CGFloat = 0

public func makeUIView(context: Context) -> VariableBlurUIView {
VariableBlurUIView(maxBlurRadius: maxBlurRadius, startOffset: startOffset, direction: direction)
VariableBlurUIView(maxBlurRadius: maxBlurRadius, direction: direction, startOffset: startOffset)
}

public func updateUIView(_ uiView: VariableBlurUIView, context: Context) {
Expand All @@ -29,7 +32,7 @@ public struct VariableBlurView: UIViewRepresentable {
/// credit https://github.com/jtrivedi/VariableBlurView
open class VariableBlurUIView: UIVisualEffectView {

public init(maxBlurRadius: CGFloat = 20, startOffset: CGFloat = -0.1, direction: VariableBlurDirection = .blurredTopClearBottom) {
public init(maxBlurRadius: CGFloat = 20, direction: VariableBlurDirection = .blurredTopClearBottom, startOffset: CGFloat = 0) {
super.init(effect: UIBlurEffect(style: .regular))

// `CAFilter` is a private QuartzCore class that we dynamically declare in `CAFilter.h`.
Expand All @@ -55,7 +58,7 @@ open class VariableBlurUIView: UIVisualEffectView {
backdropLayer?.filters = [variableBlur]

// Get rid of the visual effect view's dimming/tint view, so we don't see a hard line.
for subview in subviews[1...] {
for subview in subviews.dropFirst() {
subview.alpha = 0
}
}
Expand All @@ -64,6 +67,12 @@ open class VariableBlurUIView: UIVisualEffectView {
fatalError("init(coder:) has not been implemented")
}

open override func didMoveToWindow() {
// fixes visible pixelization at unblurred edge (https://github.com/nikstar/VariableBlur/issues/1)
guard let window, let backdropLayer = subviews.first?.layer else { return }
backdropLayer.setValue(window.screen.scale, forKey: "scale")
}

open override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
// `super.traitCollectionDidChange(previousTraitCollection)` crashes the app
}
Expand Down

0 comments on commit 7b64063

Please sign in to comment.