2022-12-05 16:21:29 +01:00
|
|
|
//
|
2022-12-07 13:20:08 +01:00
|
|
|
// TouchesVisibleWindow.swift
|
2022-12-05 16:21:29 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// Created by Chase Carroll on 12/5/22.
|
|
|
|
//
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
2022-12-06 02:22:50 +01:00
|
|
|
/// View that represents a single touch from the user.
|
2022-12-06 17:23:04 +01:00
|
|
|
private final class TouchView: UIView {
|
2022-12-05 16:21:29 +01:00
|
|
|
|
2022-12-06 17:23:04 +01:00
|
|
|
private let blurView = UIVisualEffectView(effect: nil)
|
2022-12-05 16:21:29 +01:00
|
|
|
|
|
|
|
override var frame: CGRect {
|
|
|
|
didSet {
|
|
|
|
layer.cornerRadius = frame.height / 2.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override init(frame: CGRect) {
|
|
|
|
super.init(frame: frame)
|
|
|
|
|
2022-12-06 20:42:25 +01:00
|
|
|
let isLightMode = traitCollection.userInterfaceStyle == .light
|
|
|
|
|
2022-12-05 16:21:29 +01:00
|
|
|
backgroundColor = .clear
|
|
|
|
layer.masksToBounds = true
|
|
|
|
layer.cornerCurve = .circular
|
2022-12-06 20:42:25 +01:00
|
|
|
layer.borderColor = isLightMode ? UIColor.gray.cgColor : UIColor.white.cgColor
|
2022-12-05 16:21:29 +01:00
|
|
|
layer.borderWidth = 2.0
|
|
|
|
|
2022-12-06 20:42:25 +01:00
|
|
|
let blurEffect = isLightMode ?
|
2022-12-06 17:23:04 +01:00
|
|
|
UIBlurEffect(style: .systemUltraThinMaterialDark) :
|
|
|
|
UIBlurEffect(style: .systemUltraThinMaterialLight)
|
|
|
|
blurView.effect = blurEffect
|
2022-12-05 16:21:29 +01:00
|
|
|
addSubview(blurView)
|
|
|
|
}
|
|
|
|
|
|
|
|
@available(iOS, unavailable)
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
override func layoutSubviews() {
|
|
|
|
super.layoutSubviews()
|
|
|
|
blurView.frame = bounds
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-06 02:22:50 +01:00
|
|
|
/// `UIWindow` subclass that renders visual representations of the user's touches.
|
2022-12-05 16:21:29 +01:00
|
|
|
public final class TouchesVisibleWindow: UIWindow {
|
|
|
|
|
2022-12-05 16:32:10 +01:00
|
|
|
public var touchesVisible = false {
|
2022-12-05 16:21:29 +01:00
|
|
|
didSet {
|
|
|
|
if !touchesVisible {
|
|
|
|
cleanUpAllTouches()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-06 02:22:50 +01:00
|
|
|
private var touchViews: [UITouch : TouchView] = [:]
|
2022-12-05 16:21:29 +01:00
|
|
|
|
2022-12-06 02:22:50 +01:00
|
|
|
private func newTouchView() -> TouchView {
|
2022-12-05 16:21:29 +01:00
|
|
|
let touchSize = 44.0
|
|
|
|
return TouchView(frame: CGRect(
|
|
|
|
origin: .zero,
|
|
|
|
size: CGSize(
|
|
|
|
width: touchSize,
|
|
|
|
height: touchSize
|
|
|
|
)
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2022-12-06 02:22:50 +01:00
|
|
|
private func cleanupTouch(_ touch: UITouch) {
|
2022-12-05 16:21:29 +01:00
|
|
|
guard let touchView = touchViews[touch] else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
touchView.removeFromSuperview()
|
|
|
|
touchViews.removeValue(forKey: touch)
|
|
|
|
}
|
|
|
|
|
2022-12-06 02:22:50 +01:00
|
|
|
private func cleanUpAllTouches() {
|
2022-12-05 16:21:29 +01:00
|
|
|
for (_, touchView) in touchViews {
|
|
|
|
touchView.removeFromSuperview()
|
|
|
|
}
|
|
|
|
|
|
|
|
touchViews.removeAll()
|
|
|
|
}
|
|
|
|
|
|
|
|
public override func sendEvent(_ event: UIEvent) {
|
2022-12-06 02:22:50 +01:00
|
|
|
if touchesVisible {
|
|
|
|
let touches = event.allTouches
|
|
|
|
|
|
|
|
guard
|
|
|
|
let touches = touches,
|
|
|
|
touches.count > 0
|
|
|
|
else {
|
|
|
|
cleanUpAllTouches()
|
|
|
|
super.sendEvent(event)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for touch in touches {
|
|
|
|
let touchLocation = touch.location(in: self)
|
|
|
|
switch touch.phase {
|
|
|
|
case .began:
|
|
|
|
let touchView = newTouchView()
|
|
|
|
touchView.center = touchLocation
|
|
|
|
addSubview(touchView)
|
|
|
|
touchViews[touch] = touchView
|
|
|
|
|
|
|
|
case .moved:
|
2022-12-07 13:19:48 +01:00
|
|
|
if let touchView = touchViews[touch] {
|
|
|
|
touchView.center = touchLocation
|
2022-12-06 02:22:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
case .ended, .cancelled:
|
|
|
|
cleanupTouch(touch)
|
|
|
|
|
|
|
|
default:
|
|
|
|
break
|
2022-12-05 16:21:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
super.sendEvent(event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|