-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathXYZBlurEffectView.swift
53 lines (44 loc) · 1.25 KB
/
XYZBlurEffectView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
// XYZBlurEffectView.swift
// XYZKit
//
// Created by 大大东 on 2024/2/2.
//
import Foundation
@available(iOS 11.0, *)
public class XYZBlurEffectView: UIView {
override public func layoutSubviews() {
super.layoutSubviews()
effectView?.frame = bounds
}
deinit {
animator?.stopAnimation(true)
}
/// 配置模糊参数
/// - Parameters:
/// - style: 模糊类型
/// - level: 模糊程度 [0, 1]
public func config(style: UIBlurEffect.Style, level: Float) {
if animator != nil {
animator?.stopAnimation(true)
}
if effectView != nil {
effectView?.removeFromSuperview()
effectView = nil
}
effectView = UIVisualEffectView(frame: bounds)
addSubview(effectView!)
animator = UIViewPropertyAnimator(duration: 0, curve: .linear) { [weak self] in
self?.effectView?.effect = UIBlurEffect(style: style)
}
var level = level
if level > 1 {
level = 1
} else if level < 0 {
level = 0
}
animator!.fractionComplete = CGFloat(level)
}
private var effectView: UIVisualEffectView?
private var animator: UIViewPropertyAnimator?
}