Last active
June 5, 2024 07:00
-
-
Save darrarski/29a2a4515508e385c90b3ffe6f975df7 to your computer and use it in GitHub Desktop.
UIVisualEffectView subclass that allows to customise effect intensity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
final class CustomIntensityVisualEffectView: UIVisualEffectView { | |
/// Create visual effect view with given effect and its intensity | |
/// | |
/// - Parameters: | |
/// - effect: visual effect, eg UIBlurEffect(style: .dark) | |
/// - intensity: custom intensity from 0.0 (no effect) to 1.0 (full effect) using linear scale | |
init(effect: UIVisualEffect, intensity: CGFloat) { | |
theEffect = effect | |
customIntensity = intensity | |
super.init(effect: nil) | |
} | |
required init?(coder aDecoder: NSCoder) { nil } | |
deinit { | |
animator?.stopAnimation(true) | |
} | |
override func draw(_ rect: CGRect) { | |
super.draw(rect) | |
effect = nil | |
animator?.stopAnimation(true) | |
animator = UIViewPropertyAnimator(duration: 1, curve: .linear) { [unowned self] in | |
self.effect = theEffect | |
} | |
animator?.fractionComplete = customIntensity | |
} | |
private let theEffect: UIVisualEffect | |
private let customIntensity: CGFloat | |
private var animator: UIViewPropertyAnimator? | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for cells and view states, you can finish animation