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 #42 from morizotter/shows-log
Browse files Browse the repository at this point in the history
Shows log
  • Loading branch information
morizotter committed May 6, 2015
2 parents acc9f96 + 2600a2c commit e89da93
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 27 deletions.
1 change: 1 addition & 0 deletions Example/Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
// config.color = UIColor.purpleColor()
// config.showsTimer = true
// config.showsTouchRadius = true
// config.showsLog = true
// TouchVisualizer.start(config)

return true
Expand Down
21 changes: 21 additions & 0 deletions Example/Example/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@
</subviews>
</tableViewCellContentView>
</tableViewCell>
<tableViewCell contentMode="scaleToFill" selectionStyle="none" indentationWidth="10" reuseIdentifier="Cell" textLabel="tEe-kH-vn6" detailTextLabel="LeT-lN-Pew" style="IBUITableViewCellStyleValue1" id="Brb-4E-Fk2">
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="Brb-4E-Fk2" id="8U1-JI-W8K">
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Log" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="tEe-kH-vn6">
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="false" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="LeT-lN-Pew">
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<color key="textColor" red="0.5568627451" green="0.5568627451" blue="0.57647058819999997" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
</tableViewCellContentView>
</tableViewCell>
</cells>
</tableViewSection>
<tableViewSection headerTitle="Color" id="aH2-zl-zBH">
Expand Down Expand Up @@ -126,6 +146,7 @@
<connections>
<outlet property="blueColorCell" destination="34A-MD-1d4" id="BPE-38-WT0"/>
<outlet property="greenColorCell" destination="coC-Q3-Snd" id="GZM-Re-dBZ"/>
<outlet property="logCell" destination="Brb-4E-Fk2" id="kIm-Cl-yHA"/>
<outlet property="redColorCell" destination="8EW-9h-JgJ" id="2ex-7v-ecD"/>
<outlet property="timerCell" destination="Qv0-Nv-vdg" id="JRw-pB-jrs"/>
<outlet property="touchRadiusCell" destination="Sm6-u8-Lxc" id="jM3-OP-7dh"/>
Expand Down
9 changes: 8 additions & 1 deletion Example/Example/ConfigViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ final class ConfigViewController: UITableViewController {

@IBOutlet weak var timerCell: UITableViewCell!
@IBOutlet weak var touchRadiusCell: UITableViewCell!
@IBOutlet weak var logCell: UITableViewCell!

@IBOutlet weak var blueColorCell: UITableViewCell!
@IBOutlet weak var redColorCell: UITableViewCell!
Expand Down Expand Up @@ -58,6 +59,9 @@ final class ConfigViewController: UITableViewController {
}
config.showsTouchRadius = !config.showsTouchRadius
}
if cell == logCell {
config.showsLog = !config.showsLog
}
if cell == blueColorCell {
config.color = colors["blue"]!
}
Expand All @@ -73,7 +77,7 @@ final class ConfigViewController: UITableViewController {
}

func updateCells() {
let boolCells = [timerCell, touchRadiusCell]
let boolCells = [timerCell, touchRadiusCell, logCell]
for cell in boolCells {
cell.detailTextLabel?.text = "false"
}
Expand All @@ -88,6 +92,9 @@ final class ConfigViewController: UITableViewController {
if config.showsTouchRadius {
touchRadiusCell.detailTextLabel?.text = "true"
}
if config.showsLog {
logCell.detailTextLabel?.text = "true"
}
if config.color == colors["blue"] {
blueColorCell.accessoryType = .Checkmark
}
Expand Down
64 changes: 61 additions & 3 deletions Pod/Classes/TouchVisualizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ final public class TouchVisualizer {
private var touchViews = [TouchView]()
private var enabled:Bool = false

private var previousLog = ""

static let sharedInstance = TouchVisualizer()

private init() {
Expand All @@ -38,6 +40,7 @@ final public class TouchVisualizer {
}

// MARK: - Methods

public class func isEnabled() -> Bool {
return sharedInstance.enabled
}
Expand Down Expand Up @@ -107,7 +110,6 @@ final public class TouchVisualizer {
let keyWindow = UIApplication.sharedApplication().keyWindow!

for touch in event.allTouches()! as! Set<UITouch> {

let phase = touch.phase
switch phase {
case .Began:
Expand All @@ -117,22 +119,78 @@ final public class TouchVisualizer {
view.beginTouch()
view.center = touch.locationInView(keyWindow)
keyWindow.addSubview(view)
log(touch)
case .Moved:
if let view = findTouchView(touch) {
view.center = touch.locationInView(keyWindow)
}
log(touch)
case .Stationary:
log(touch)
break
case .Ended, .Cancelled:
if let view = findTouchView(touch) {
UIView.animateWithDuration(0.2, delay: 0.0, options: .AllowUserInteraction, animations: { () -> Void in
UIView.animateWithDuration(0.2, delay: 0.0, options: .AllowUserInteraction, animations: { [unowned self] () -> Void in
view.alpha = 0.0
view.endTouch()
}, completion: { (finished) -> Void in
}, completion: { [unowned self] (finished) -> Void in
view.removeFromSuperview()
self.log(touch)
});
}
log(touch)
}
}
}

public func log(touch: UITouch) {
if !config.showsLog {
return
}

var ti = 0
var viewLogs = [[String:String]]()
for view in touchViews {
var index = ""
if view.superview != nil {
index = "\(ti)"
++ti
}

var phase = ""
switch touch.phase {
case .Began: phase = "B"
case .Moved: phase = "M"
case .Ended: phase = "E"
case .Cancelled: phase = "C"
case .Stationary: phase = "S"
}

let x = String(format: "%.02f", Float(view.center.x))
let y = String(format: "%.02f", Float(view.center.y))
let center = "(\(x), \(y))"

let radius = String(format: "%.02f", Float(touch.majorRadius))

viewLogs.append(["index": index, "center": center, "phase": phase, "radius": radius])
}

var log = "TV: "
for viewLog in viewLogs {
if count(viewLog["index"]!) == 0 {
continue
}
let index = viewLog["index"]!
let center = viewLog["center"]!
let phase = viewLog["phase"]!
let radius = viewLog["radius"]!
log += "[\(index)]<\(phase)> c:\(center) r:\(radius)\t"
}

if previousLog == log {
return
}
previousLog = log
println(log)
}
}
28 changes: 5 additions & 23 deletions Pod/Classes/TouchVisualizerConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,10 @@ public struct TouchVisualizerConfig {
*/
public var showsTouchRadius = false

/**
Shows log. This will affect performance. Make sure showing logs only in development environment.
*/
public var showsLog = false

public init() {}
public mutating func setColor(color: UIColor) {
self.color = color
}
public mutating func setImage(image: UIImage) {
self.image = image
}
public mutating func setShowsTimer(shows: Bool) {
self.showsTimer = shows
}
public mutating func setShowsTouchRadius(shows: Bool) {
self.showsTouchRadius = shows
}
public mutating func setDefaultSize(size: CGSize) {
var newSize = size
if size.width != size.height {
if newSize.width > newSize.height {
newSize.height = newSize.width
} else {
newSize.width = newSize.height
}
}
self.defaultSize = newSize
}
}

0 comments on commit e89da93

Please sign in to comment.