forked from mapbox/Fingertips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MBFingerTipWindow.swift
275 lines (212 loc) · 8.64 KB
/
MBFingerTipWindow.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//
// MBFingerTipWindow.swift
// MietappFT
//
// Created by Felix Traxler on 05.08.19.
// Copyright © 2019 Felix Traxler. All rights reserved.
//
import Foundation
import UIKit
class MBFingerTipView: UIImageView {
var timestamp: TimeInterval?
var shouldAutomaticallyRemoveAfterTimeout: Bool?
var fadingOut: Bool = false
}
class MBFingerTipOverlayWindow: UIWindow {
override var rootViewController: UIViewController? {
set {
super.rootViewController = newValue
}
get {
for window in UIApplication.shared.windows {
if let window = window as? MBFingerTipWindow {
return window.rootViewController
}
}
return super.rootViewController
}
}
}
class MBFingerTipWindow: UIWindow {
var touchAlpha: CGFloat = 0.5
var fadeDuration: TimeInterval = 0.3
var strokeColor: UIColor = .black
var fillColor: UIColor = .white
private var active: Bool = false
// if set to 'true' the touches are shown even when no external screen is connected
var alwaysShowTouches: Bool = false {
didSet {
if oldValue != alwaysShowTouches {
updateFingertipsAreActive()
}
}
}
private var _touchImage: UIImage? = nil
var touchImage: UIImage {
if _touchImage == nil {
let clipPath = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 50, height: 50))
UIGraphicsBeginImageContextWithOptions(clipPath.bounds.size, false, 0)
let drawPath = UIBezierPath(arcCenter: CGPoint(x: 25, y: 25), radius: 22, startAngle: 0, endAngle: 2 * CGFloat.pi, clockwise: true)
drawPath.lineWidth = 2
strokeColor.setStroke()
fillColor.setFill()
drawPath.stroke()
drawPath.fill()
clipPath.addClip()
_touchImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
return _touchImage!
}
private var _overlayWindow: UIWindow?
var overlayWindow: UIWindow {
get {
if _overlayWindow == nil {
_overlayWindow = MBFingerTipOverlayWindow(frame: frame)
_overlayWindow?.isUserInteractionEnabled = false
_overlayWindow?.windowLevel = UIWindow.Level.statusBar
_overlayWindow?.backgroundColor = .clear
_overlayWindow?.isHidden = false
}
return _overlayWindow!
}
set {
}
}
var action: Bool?
var fingerTipRemovalScheduled: Bool = false
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
func commonInit() {
NotificationCenter.default.addObserver(self, selector: #selector(screenConnect), name: UIScreen.didConnectNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(screenDisconnect), name: UIScreen.didDisconnectNotification, object: nil)
updateFingertipsAreActive()
}
deinit {
NotificationCenter.default.removeObserver(self, name: UIScreen.didConnectNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIScreen.didDisconnectNotification, object: nil)
}
func anyScreenIsMirrored() -> Bool {
if UIScreen.instancesRespond(to: #selector(getter: UIScreen.mirrored)) {
for screen in UIScreen.screens {
if let _ = screen.mirrored {
return true
}
}
}
return false
}
func updateFingertipsAreActive() {
if alwaysShowTouches {
active = true
} else {
active = anyScreenIsMirrored()
}
}
override func sendEvent(_ event: UIEvent) {
if active {
guard let allTouches: Set<UITouch> = event.allTouches else { return }
for touch in allTouches {
switch touch.phase {
case .began, .moved, .stationary:
var touchView = overlayWindow.viewWithTag(touch.hashValue) as? MBFingerTipView
if touch.phase != .stationary && touchView != nil && touchView?.fadingOut == true {
touchView?.removeFromSuperview()
touchView = nil
}
if touchView == nil && touch.phase != .stationary {
touchView = MBFingerTipView(image: touchImage)
overlayWindow.addSubview(touchView!)
}
if touchView?.fadingOut == false {
touchView?.alpha = touchAlpha
touchView?.center = touch.location(in: overlayWindow)
touchView?.tag = touch.hashValue
touchView?.timestamp = touch.timestamp
touchView?.shouldAutomaticallyRemoveAfterTimeout = shouldAutomaticallyRemoveFingerTip(for: touch)
}
break
case .ended, .cancelled:
removeFingerTip(with: touch.hashValue, animated: true)
break
@unknown default:
break
}
}
}
super.sendEvent(event)
scheduleFingerTipRemoval()
}
func scheduleFingerTipRemoval() {
if fingerTipRemovalScheduled {
return
}
fingerTipRemovalScheduled = true
perform(#selector(removeInactiveFingerTips), with: nil, afterDelay: 0.1)
}
func cancelScheduledFingerTipRemoval() {
fingerTipRemovalScheduled = true
NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(removeInactiveFingerTips), object: nil)
}
@objc func removeInactiveFingerTips() {
fingerTipRemovalScheduled = false
let now = ProcessInfo.processInfo.systemUptime
let REMOVAL_DELAY = 0.2
for touchView in overlayWindow.subviews {
if let touchView = touchView as? MBFingerTipView {
if touchView.shouldAutomaticallyRemoveAfterTimeout == true && now > touchView.timestamp ?? 0 + REMOVAL_DELAY {
removeFingerTip(with: touchView.tag, animated: true)
}
}
}
if overlayWindow.subviews.count > 0 {
scheduleFingerTipRemoval()
}
}
func removeFingerTip(with hash: Int, animated: Bool) {
guard let touchView = overlayWindow.viewWithTag(hash) as? MBFingerTipView else { return }
if touchView.fadingOut == true {
return
}
UIView.animate(withDuration: fadeDuration) {
touchView.alpha = 0
touchView.frame = CGRect(x: touchView.center.x - touchView.frame.size.width / 1.5,
y: touchView.center.y - touchView.frame.size.height / 1.5,
width: touchView.frame.size.width * 1.5,
height: touchView.frame.size.height * 1.5)
}
touchView.fadingOut = true
touchView.perform(#selector(removeFromSuperview), with: nil, afterDelay: fadeDuration)
}
func shouldAutomaticallyRemoveFingerTip(for touch: UITouch) -> Bool {
var view = touch.view
view = view?.hitTest(touch.location(in: view), with: nil)
while view != nil {
if view is UITableViewCell {
for recognizer in touch.gestureRecognizers ?? [] {
if recognizer is UISwipeGestureRecognizer {
return true
}
}
} else if view is UITableView {
if touch.gestureRecognizers?.count == 0 {
return true
}
}
view = view?.superview
}
return false
}
@objc func screenConnect() {
updateFingertipsAreActive()
}
@objc func screenDisconnect() {
updateFingertipsAreActive()
}
}