[英]How to reduce blur effect on UIVisualEffectView
我們可以使用 animator 完全本機地做到這一點,並具有正確的預期外觀
用法:
let blurEffectView = BlurEffectView()
view.addSubview(blurEffectView)
BlurEffectView 實現:
class BlurEffectView: UIVisualEffectView {
var animator = UIViewPropertyAnimator(duration: 1, curve: .linear)
override func didMoveToSuperview() {
guard let superview = superview else { return }
backgroundColor = .clear
frame = superview.bounds //Or setup constraints instead
setupBlur()
}
private func setupBlur() {
animator.stopAnimation(true)
effect = nil
animator.addAnimations { [weak self] in
self?.effect = UIBlurEffect(style: .dark)
}
animator.fractionComplete = 0.1 //This is your blur intensity in range 0 - 1
}
deinit {
animator.stopAnimation(true)
}
}
這是我對 bodich 的回答的重復,它解決了每次應用程序進入前台時模糊重置的問題。 我仍然不相信這個 hack 是可交付的,因為 CoreAnimation 可能在其他時候決定完成陳舊的動畫,在這種情況下模糊會突然變得完全強度。
class BlurEffectView: UIVisualEffectView {
private var animator = UIViewPropertyAnimator(duration: 1, curve: .linear)
private var intensity: CGFloat = 0.25
init(intensity: CGFloat) {
self.intensity = intensity
super.init(effect: nil)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
deinit {
animator.stopAnimation(true)
}
override func didMoveToSuperview() {
guard let superview = superview else { return }
backgroundColor = .clear
frame = superview.bounds
autoresizingMask = [.flexibleWidth, .flexibleHeight]
clipsToBounds = true
NotificationCenter.default.addObserver(
self,
selector: #selector(appWillEnterFG(_:)),
name:UIApplication.willEnterForegroundNotification,
object: nil
)
setUpAnimation()
}
private func setUpAnimation() {
animator.stopAnimation(true)
effect = nil
animator.addAnimations { [weak self] in
self?.effect = UIBlurEffect(style: .light)
}
animator.fractionComplete = intensity
}
@objc func appWillEnterFG(_ note: Notification) {
setUpAnimation()
}
}
我知道這已經晚了,但是如果您只是將模糊用於模態演示,Apple 有一個內置的 UIModalPresentationStyle,稱為 blurOverFullScreen,並且效果很好。
只需將父控制器的 modalPresentationStyle 設置為 .blurOverFullScreen 並呈現新的視圖控制器。 如果新視圖小於屏幕,背景應該像圖片中看到的那樣模糊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.