Skip to content
This repository has been archived by the owner on May 23, 2022. It is now read-only.

Commit

Permalink
Merge pull request #75 from morizotter/morizotter/add-demo-for-touches
Browse files Browse the repository at this point in the history
Add demo for touches
  • Loading branch information
morizotter authored May 3, 2017
2 parents 56683a9 + bb878a6 commit e6b2bc0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
26 changes: 26 additions & 0 deletions Example/Example/DetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class DetailViewController: UIViewController {

var text: String?

weak var timer: Timer?

@IBOutlet weak private var textLabel: UILabel!

override func viewDidLoad() {
Expand All @@ -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))")
}
}
}
21 changes: 10 additions & 11 deletions TouchVisualizer/TouchView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,23 @@ 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()
}
}

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
}
}
}
21 changes: 10 additions & 11 deletions TouchVisualizer/Visualizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -125,7 +126,7 @@ extension Visualizer {
return nil
}

public func handleEvent(_ event: UIEvent) {
open func handleEvent(_ event: UIEvent) {
if event.type != .touches {
return
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit e6b2bc0

Please sign in to comment.