|
8 | 8 |
|
9 | 9 | import Foundation |
10 | 10 |
|
| 11 | +fileprivate let startScale: CGFloat = 2 |
| 12 | +fileprivate let endScale: CGFloat = 1.2 |
| 13 | + |
| 14 | +/// Foobar |
11 | 15 | public class VisibleTouchWindow: UIWindow { |
12 | 16 |
|
| 17 | + // MARK: - Properties |
| 18 | + |
| 19 | + public var isShowTouchesEnabled: Bool = true |
| 20 | + public var appearAnimationDuration: Double = 0.1 |
| 21 | + public var movementAnimationDuration: Double = 0.1 |
| 22 | + public var disappearAnimationDuration: Double = 0.1 |
| 23 | + private let pointerSize = CGSize(width: 50, height: 50) |
| 24 | + |
| 25 | + // MARK: - Lifecycle |
| 26 | + |
13 | 27 | override public init(frame: CGRect) { |
14 | 28 | super.init(frame: frame) |
15 | | - |
16 | | - // stub |
17 | 29 | } |
18 | 30 |
|
19 | 31 | required public init?(coder aDecoder: NSCoder) { |
20 | 32 | fatalError("init(coder:) has not been implemented") |
21 | 33 | } |
22 | 34 |
|
| 35 | + // MARK: - Event Handling |
| 36 | + |
| 37 | + override public func sendEvent(_ event: UIEvent) { |
| 38 | + super.sendEvent(event) |
| 39 | + |
| 40 | + guard event.type == .touches, let allTouches = event.allTouches else { |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + for touch in allTouches { |
| 45 | + switch touch.phase { |
| 46 | + case .began: |
| 47 | + // Don't display new touches if showTouches is disabled |
| 48 | + guard isShowTouchesEnabled else { |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + // Create touch point view |
| 53 | + let point = touch.location(in: self) |
| 54 | + let origin = CGPoint(x: point.x - pointerSize.width / 2, |
| 55 | + y: point.y - pointerSize.height / 2) |
| 56 | + let touchPointView = TouchPointView(frame: CGRect(origin: origin, size: pointerSize)) |
| 57 | + touchPointView.transform = CGAffineTransform(scaleX: startScale, y: startScale) |
| 58 | + touchPointView.alpha = 0 |
| 59 | + |
| 60 | + // Store view with touch event |
| 61 | + touch.touchPointView = touchPointView |
| 62 | + |
| 63 | + // Display touch with animation |
| 64 | + addSubview(touchPointView) |
| 65 | + UIView.animate(withDuration: appearAnimationDuration) { |
| 66 | + touchPointView.transform = CGAffineTransform.identity |
| 67 | + touchPointView.alpha = 1 |
| 68 | + } |
| 69 | + |
| 70 | + case .ended, .cancelled: |
| 71 | + // Retrieve touch point view from event |
| 72 | + if let touchPointView = touch.touchPointView { |
| 73 | + // Animate removal |
| 74 | + UIView.animate(withDuration: disappearAnimationDuration, animations: { |
| 75 | + touchPointView.transform = CGAffineTransform(scaleX: endScale, y: endScale) |
| 76 | + touchPointView.alpha = 0; |
| 77 | + }, completion: { (_) in |
| 78 | + touchPointView.removeFromSuperview() |
| 79 | + }) |
| 80 | + } |
| 81 | + break |
| 82 | + |
| 83 | + case .moved: |
| 84 | + // Retrieve touch point view from event |
| 85 | + if let touchPointView = touch.touchPointView { |
| 86 | + // Determine point's new frame based on touch event |
| 87 | + let point = touch.location(in: self) |
| 88 | + var touchFrame = touchPointView.frame |
| 89 | + touchFrame.origin.x = point.x - pointerSize.width / 2 |
| 90 | + touchFrame.origin.y = point.y - pointerSize.height / 2 |
| 91 | + |
| 92 | + // Animate movement |
| 93 | + UIView.animate(withDuration: movementAnimationDuration) { |
| 94 | + touchPointView.frame = touchFrame |
| 95 | + } |
| 96 | + } |
| 97 | + break |
| 98 | + |
| 99 | + default: |
| 100 | + break |
| 101 | + } |
| 102 | + } |
| 103 | + } |
23 | 104 | } |
0 commit comments