-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVolumer.swift
More file actions
144 lines (123 loc) · 3.83 KB
/
Volumer.swift
File metadata and controls
144 lines (123 loc) · 3.83 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import UIKit
import MediaPlayer
public enum Volume
{
public typealias Block = () -> Void
case up, down
public static func when(_ kind: Volume, _ block: @escaping Block)
{
Volume.setup()
var array = Volume.blocks[kind] ?? []
array.append(block)
Volume.blocks[kind] = array
}
public func when(block: @escaping Block)
{
Volume.when(self, block)
}
public static func reset() {
Volume.blocks = [Volume: [Block]]()
self.unsetup()
}
public static var keepIntact = true
private static var blocks = [Volume: [Block]]()
private static var hasSetup = false
private static var volumeView: MPVolumeView!
public static func use(volumeView: MPVolumeView)
{
self.volumeView = volumeView
}
private static var observer: Observer!
public static func setup()
{
if self.hasSetup {
return
}
self.hasSetup = true
if self.volumeView == nil {
self.volumeView = MPVolumeView(frame: CGRect(x: 0,
y: -100,
width: 100,
height: 50))
UIApplication.shared.windows.first!.addSubview(self.volumeView!)
}
if self.observer == nil {
self.observer = Observer()
}
self.volumeView?.slider?.addTarget(self.observer,
action: #selector(Observer.sliderDidChange(_:)),
for: .valueChanged)
}
public static func unsetup()
{
if !self.hasSetup {
return
}
self.hasSetup = false
self.volumeView?.slider?.removeTarget(self.observer,
action: #selector(Observer.sliderDidChange(_:)),
for: .valueChanged)
self.observer = nil
self.volumeView.removeFromSuperview()
self.volumeView = nil
}
//MARK: Observer Delegate
fileprivate static func didChange(up: Bool) {
if let tempBlocks = Volume.blocks[up.asVolume] {
for block in tempBlocks {
DispatchQueue.main.async {
block()
}
}
}
}
}
@objc private class Observer: NSObject
{
private var currentValue: Float?
private var notify = true
deinit {
NotificationCenter.default.removeObserver(self)
}
override init() {
super.init()
NotificationCenter.default.addObserver(self,
selector: #selector(Observer.willBecomeInactive(_:)),
name: UIApplication.willResignActiveNotification,
object: nil)
}
@objc fileprivate func sliderDidChange(_ sender: UISlider) {
if currentValue == nil {
currentValue = sender.value
return
}
if !notify {
notify = true
return
}
if sender.value == currentValue {
return
}
Volume.didChange(up: sender.value > currentValue!)
currentValue = sender.value
if Volume.keepIntact {
notify = false
sender.value = currentValue!
}
}
@objc fileprivate func willBecomeInactive(_ notification: NSNotification) {
currentValue = nil
}
}
private extension MPVolumeView
{
var slider: UISlider? {
return self.subviews.first(where: { $0 is UISlider }) as? UISlider
}
}
private extension Bool
{
var asVolume: Volume {
return self ? .up : .down
}
}