Skip to content

Instantly share code, notes, and snippets.

@darrarski
Last active November 11, 2024 09:25
Show Gist options
  • Save darrarski/29a2a4515508e385c90b3ffe6f975df7 to your computer and use it in GitHub Desktop.
Save darrarski/29a2a4515508e385c90b3ffe6f975df7 to your computer and use it in GitHub Desktop.
UIVisualEffectView subclass that allows to customise effect intensity
// MIT License
//
// Copyright (c) 2017 Dariusz Rybicki Darrarski
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
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?
}
@MonteMango
Copy link

great

@tallot13
Copy link

ios 14.5 from background - effect reset

@billidani7
Copy link

In order to solve the foreground/background issues try setting animator?.pausesOnCompletion = true inside func draw(_ rect: CGRect) after animator = UIViewPropertyAnimator(.....

@dafurman
Copy link

dafurman commented Jun 28, 2022

Just as a heads up, this code has trouble with UITests.

Setting fractionComplete on an animator and not stopping the animation essentially indicates to the test runner that animations haven't settled, which results in tests hanging for long periods of time, showing the following message in the console:
Wait for <myAppIdentifier> to idle

From the docs on fractionComplete:

Changing the value of this property on an inactive animator moves it to the active state.

There's a bit more info on this here that I found helpful in solving this code's behavior with UITests:
https://diggingdeveloper.blog/2022/01/21/xcode-ui-tests-infinite-wait-for-app-to-idle/

As a workaround, you could choose to disable this code with a custom environment variable like isUITesting.

@tiskender2
Copy link

tiskender2 commented Jan 19, 2024

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
    DispatchQueue.main.async { [weak self] in
            self?.animator.stopAnimation(true)
            self?.animator.finishAnimation(at: .current)
        }
  }

for cells and view states, you can finish animation

@seyoung-hyun
Copy link

Thanks your code :)
Can I use your code in my commercial app?
Please let me know how I can use the code for commercial distribution.

@darrarski
Copy link
Author

Thanks your code :) Can I use your code in my commercial app? Please let me know how I can use the code for commercial distribution.

@seyoung-hyun you are welcome!

Feel free to use the code in your commercial projects. To avoid confusion, I've added standard MIT licence. It shall be included in all copies or substantial portions of the gist. Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment