From bb878a6abbd31ae80382c1d0409db4d41e0d2dec Mon Sep 17 00:00:00 2001 From: Naoki Morita Date: Thu, 4 May 2017 03:04:30 +0900 Subject: [PATCH] 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 }