From 2dfce5ba510c245b25920a8707f9d1ca56cb3344 Mon Sep 17 00:00:00 2001 From: Naoki Morita Date: Thu, 4 May 2017 02:52:36 +0900 Subject: [PATCH 1/2] Tweak Visualizer --- TouchVisualizer/TouchView.swift | 21 ++++++++++----------- TouchVisualizer/Visualizer.swift | 19 +++++++++---------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/TouchVisualizer/TouchView.swift b/TouchVisualizer/TouchView.swift index 09d0c8f..ee53b8c 100644 --- a/TouchVisualizer/TouchView.swift +++ b/TouchVisualizer/TouchView.swift @@ -91,11 +91,11 @@ final public class TouchView: UIImageView { // MARK: - Update Functions internal func update(_ timer: Timer) { - if let startDate = startDate { - let interval = Date().timeIntervalSince(startDate) - let timeString = String(format: "%.02f", Float(interval)) - timerLabel.text = timeString - } + guard let startDate = startDate else { return } + + let interval = Date().timeIntervalSince(startDate) + let timeString = String(format: "%.02f", Float(interval)) + timerLabel.text = timeString if _config.showsTouchRadius { updateSize() @@ -103,12 +103,11 @@ final public class TouchView: UIImageView { } internal func updateSize() { - if let touch = touch { - let ratio = touch.majorRadius * 2.0 / _config.defaultSize.width - if ratio != previousRatio { - layer.transform = CATransform3DMakeScale(ratio, ratio, 1.0) - previousRatio = ratio - } + guard let touch = touch else { return } + let ratio = touch.majorRadius * 2.0 / _config.defaultSize.width + if ratio != previousRatio { + layer.transform = CATransform3DMakeScale(ratio, ratio, 1.0) + previousRatio = ratio } } } diff --git a/TouchVisualizer/Visualizer.swift b/TouchVisualizer/Visualizer.swift index 6555dfc..5c6c7ac 100644 --- a/TouchVisualizer/Visualizer.swift +++ b/TouchVisualizer/Visualizer.swift @@ -92,7 +92,8 @@ extension Visualizer { let instance = sharedInstance var touches: [UITouch] = [] for view in instance.touchViews { - touches.append(view.touch!) + guard let touch = view.touch else { continue } + touches.append(touch) } return touches } @@ -161,7 +162,6 @@ extension Visualizer { log(touch) case .stationary: log(touch) - break case .ended, .cancelled: if let view = findTouchView(touch) { UIView.animate(withDuration: 0.2, delay: 0.0, options: .allowUserInteraction, animations: { () -> Void in @@ -192,23 +192,21 @@ extension Visualizer { return } - var ti = 0.0 + var ti = 0 var viewLogs = [[String:String]]() for view in touchViews { var index = "" - if view.superview != nil { - index = "\(ti)" - ti += 1 - } + index = "\(ti)" + ti += 1 var phase: String! switch touch.phase { case .began: phase = "B" case .moved: phase = "M" + case .stationary: phase = "S" case .ended: phase = "E" case .cancelled: phase = "C" - case .stationary: phase = "S" } let x = String(format: "%.02f", view.center.x) @@ -218,7 +216,8 @@ extension Visualizer { viewLogs.append(["index": index, "center": center, "phase": phase, "radius": radius]) } - var log = "TV: " + var log = "" + for viewLog in viewLogs { if (viewLog["index"]!).characters.count == 0 { @@ -229,7 +228,7 @@ extension Visualizer { let center = viewLog["center"]! let phase = viewLog["phase"]! let radius = viewLog["radius"]! - log += "[\(index)]<\(phase)> c:\(center) r:\(radius)\t" + log += "Touch: [\(index)]<\(phase)> c:\(center) r:\(radius)\t\n" } if log == previousLog { From bb878a6abbd31ae80382c1d0409db4d41e0d2dec Mon Sep 17 00:00:00 2001 From: Naoki Morita Date: Thu, 4 May 2017 03:04:30 +0900 Subject: [PATCH 2/2] Add getTouches demo --- Example/Example/DetailViewController.swift | 26 ++++++++++++++++++++++ TouchVisualizer/Visualizer.swift | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Example/Example/DetailViewController.swift b/Example/Example/DetailViewController.swift index 0068ce6..e201f40 100644 --- a/Example/Example/DetailViewController.swift +++ b/Example/Example/DetailViewController.swift @@ -12,6 +12,8 @@ class DetailViewController: UIViewController { var text: String? + weak var timer: Timer? + @IBOutlet weak private var textLabel: UILabel! override func viewDidLoad() { @@ -20,4 +22,28 @@ class DetailViewController: UIViewController { self.textLabel.text = "No.\(text) cell is tapped." } } + + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + + timer = Timer.scheduledTimer(timeInterval: 1.0/60.0, target: self, selector: #selector(logTouches(timer:)), userInfo: nil, repeats: true) + } + + override func viewDidDisappear(_ animated: Bool) { + super.viewDidDisappear(animated) + + timer?.invalidate() + timer = nil + } + + // Example for getting current touches + // * Caution: Touches here have been emmited before. For example `phase` is always `stationary`, + // it WAS `moved` at the time emmited. + // So use `getTouches` func for limited debug purpose. + // If you want to know the exact value, please override `handleEvent:` in UIWindow+Swizzle.swift. + func logTouches(timer: Timer) { + for (idx, touch) in Visualizer.getTouches().enumerated() { + print("[\(idx)] location: \(touch.location(in: self.view))") + } + } } diff --git a/TouchVisualizer/Visualizer.swift b/TouchVisualizer/Visualizer.swift index 5c6c7ac..09b1be4 100644 --- a/TouchVisualizer/Visualizer.swift +++ b/TouchVisualizer/Visualizer.swift @@ -126,7 +126,7 @@ extension Visualizer { return nil } - public func handleEvent(_ event: UIEvent) { + open func handleEvent(_ event: UIEvent) { if event.type != .touches { return }