-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathVisualEffectView.swift
111 lines (86 loc) · 3.06 KB
/
VisualEffectView.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// VisualEffectView.swift
// VisualEffectView
//
// Created by Lasha Efremidze on 5/26/16.
// Copyright © 2016 Lasha Efremidze. All rights reserved.
//
import UIKit
/// VisualEffectView is a dynamic background blur view.
@objcMembers
open class VisualEffectView: UIVisualEffectView {
/// Returns the instance of UIBlurEffect.
private let blurEffect = (NSClassFromString("_UICustomBlurEffect") as! UIBlurEffect.Type).init()
/**
Tint color.
The default value is nil.
*/
open var colorTint: UIColor? {
get {
return sourceOver?.value(forKeyPath: "color") as? UIColor
}
set {
prepareForChanges()
sourceOver?.setValue(newValue, forKeyPath: "color")
sourceOver?.perform(Selector(("applyRequestedEffectToView:")), with: overlayView)
applyChanges()
overlayView?.backgroundColor = newValue
}
}
/**
Tint color alpha.
Don't use it unless `colorTint` is not nil.
The default value is 0.0.
*/
open var colorTintAlpha: CGFloat {
get { return _value(forKey: .colorTintAlpha) ?? 0.0 }
set { colorTint = colorTint?.withAlphaComponent(newValue) }
}
/**
Blur radius.
The default value is 0.0.
*/
open var blurRadius: CGFloat {
get {
return gaussianBlur?.requestedValues?["inputRadius"] as? CGFloat ?? 0
}
set {
prepareForChanges()
gaussianBlur?.requestedValues?["inputRadius"] = newValue
applyChanges()
}
}
/**
Scale factor.
The scale factor determines how content in the view is mapped from the logical coordinate space (measured in points) to the device coordinate space (measured in pixels).
The default value is 1.0.
*/
open var scale: CGFloat {
get { return _value(forKey: .scale) ?? 1.0 }
set { _setValue(newValue, forKey: .scale) }
}
// MARK: - Initialization
public override init(effect: UIVisualEffect?) {
super.init(effect: effect)
scale = 1
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
scale = 1
}
}
// MARK: - Helpers
private extension VisualEffectView {
/// Returns the value for the key on the blurEffect.
func _value<T>(forKey key: Key) -> T? {
return blurEffect.value(forKeyPath: key.rawValue) as? T
}
/// Sets the value for the key on the blurEffect.
func _setValue<T>(_ value: T?, forKey key: Key) {
blurEffect.setValue(value, forKeyPath: key.rawValue)
}
enum Key: String {
case colorTint, colorTintAlpha, blurRadius, scale
}
}
// ["grayscaleTintLevel", "grayscaleTintAlpha", "lightenGrayscaleWithSourceOver", "colorTint", "colorTintAlpha", "colorBurnTintLevel", "colorBurnTintAlpha", "darkeningTintAlpha", "darkeningTintHue", "darkeningTintSaturation", "darkenWithSourceOver", "blurRadius", "saturationDeltaFactor", "scale", "zoom"]