forked from mapbox/Fingertips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FingerTipWindow.swift
281 lines (223 loc) · 8.72 KB
/
FingerTipWindow.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
274
275
276
277
278
279
280
// Copyright 2011-2023 Mapbox, Inc. All rights reserved.
import Foundation
import UIKit
@objc(MBXFingerTipView)
class FingerTipView: UIImageView {
var timestamp: TimeInterval?
var shouldAutomaticallyRemoveAfterTimeout: Bool?
var fadingOut: Bool = false
}
@objc (MBXFingerTipOverlayWindow)
class FingerTipOverlayWindow: UIWindow {
override var rootViewController: UIViewController? {
set {
super.rootViewController = newValue
}
get {
return FingerTipWindow.fingerTipWindow?.rootViewController ?? super.rootViewController
}
}
}
@objc (MBXFingerTipWindow)
open class FingerTipWindow: UIWindow {
public static var fingerTipWindow: FingerTipWindow? {
return UIApplication.shared.windows.compactMap({ $0 as? FingerTipWindow }).first
}
public var touchAlpha: CGFloat = 0.5
public var fadeDuration: TimeInterval = 0.3
public var strokeColor: UIColor = .black
public var fillColor: UIColor = .white
private var active: Bool = false
// if set to 'true' the touches are shown even when no external screen is connected
public 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 {
if #available(iOS 13.0, *), let windowScene = windowScene {
_overlayWindow = FingerTipOverlayWindow(windowScene: windowScene)
} else {
_overlayWindow = FingerTipOverlayWindow(frame: frame)
}
_overlayWindow?.isUserInteractionEnabled = false
_overlayWindow?.windowLevel = .statusBar
_overlayWindow?.backgroundColor = .clear
_overlayWindow?.isHidden = false
}
return _overlayWindow!
}
set {
_overlayWindow = newValue
}
}
var action: Bool?
var fingerTipRemovalScheduled: Bool = false
deinit {
NotificationCenter.default.removeObserver(self)
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override public init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
@available(iOS 13.0, *)
public override init(windowScene: UIWindowScene) {
super.init(windowScene: windowScene)
commonInit()
}
func commonInit() {
NotificationCenter.default.addObserver(self, selector: #selector(updateFingertipsAreActive), name: UIScreen.didConnectNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(updateFingertipsAreActive), name: UIScreen.didDisconnectNotification, object: nil)
if #available(iOS 11.0, *) {
NotificationCenter.default.addObserver(self, selector: #selector(updateFingertipsAreActive), name: UIScreen.capturedDidChangeNotification, object: nil)
}
updateFingertipsAreActive()
}
func anyScreenIsCaptured() -> Bool {
let capturedScreen: UIScreen?
if #available(iOS 11.0, *) {
capturedScreen = UIScreen.screens.first(where: \.isCaptured)
} else {
capturedScreen = UIScreen.screens.first(where: { $0.mirrored != nil })
}
return capturedScreen != nil
}
@objc func updateFingertipsAreActive() {
let envDebug = (ProcessInfo.processInfo.environment["DEBUG_FINGERTIP_WINDOW"] as? NSString)?.boolValue
if alwaysShowTouches || envDebug == true {
active = true
} else {
active = anyScreenIsCaptured()
}
}
public override func sendEvent(_ event: UIEvent) {
defer {
super.sendEvent(event)
scheduleFingerTipRemoval()
}
guard active else { return }
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? FingerTipView
if touch.phase != .stationary && touchView != nil && touchView?.fadingOut == true {
touchView?.removeFromSuperview()
touchView = nil
}
if touchView == nil && touch.phase != .stationary {
touchView = FingerTipView(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
case .regionEntered, .regionMoved, .regionExited:
fallthrough
@unknown default:
break
}
}
}
func scheduleFingerTipRemoval() {
if fingerTipRemovalScheduled {
return
}
fingerTipRemovalScheduled = true
perform(#selector(removeInactiveFingerTips), with: nil, afterDelay: 0.1)
}
@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? FingerTipView {
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? FingerTipView else { return }
if touchView.fadingOut == true {
return
}
let animation = {
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)
}
let completion: (Bool) -> () = { _ in
touchView.fadingOut = false
touchView.removeFromSuperview()
}
touchView.fadingOut = true
if animated {
UIView.animate(withDuration: fadeDuration, animations: animation, completion: completion)
} else {
animation()
completion(true)
}
}
func shouldAutomaticallyRemoveFingerTip(for touch: UITouch) -> Bool {
var view = touch.view
view = view?.hitTest(touch.location(in: view), with: nil)
while view != nil {
switch view {
case is UITableViewCell:
for recognizer in touch.gestureRecognizers ?? [] {
if recognizer is UISwipeGestureRecognizer {
return true
}
}
case is UITableView:
if touch.gestureRecognizers?.count == 0 {
return true
}
default: break
}
view = view?.superview
}
return false
}
}