diff --git a/Example/Example/AppDelegate.swift b/Example/Example/AppDelegate.swift index 759efef..5e887eb 100644 --- a/Example/Example/AppDelegate.swift +++ b/Example/Example/AppDelegate.swift @@ -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 diff --git a/Example/Example/Base.lproj/Main.storyboard b/Example/Example/Base.lproj/Main.storyboard index 49816bf..dea16aa 100644 --- a/Example/Example/Base.lproj/Main.storyboard +++ b/Example/Example/Base.lproj/Main.storyboard @@ -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"> @@ -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"/> diff --git a/Example/Example/ConfigViewController.swift b/Example/Example/ConfigViewController.swift index 29ec476..c7612ef 100644 --- a/Example/Example/ConfigViewController.swift +++ b/Example/Example/ConfigViewController.swift @@ -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! @@ -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"]! } @@ -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" } @@ -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 } diff --git a/Pod/Classes/TouchVisualizer.swift b/Pod/Classes/TouchVisualizer.swift index 10027a6..09498df 100644 --- a/Pod/Classes/TouchVisualizer.swift +++ b/Pod/Classes/TouchVisualizer.swift @@ -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() { @@ -38,6 +40,7 @@ final public class TouchVisualizer { } // MARK: - Methods + public class func isEnabled() -> Bool { return sharedInstance.enabled } @@ -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: @@ -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) } } diff --git a/Pod/Classes/TouchVisualizerConfig.swift b/Pod/Classes/TouchVisualizerConfig.swift index 54d1b3d..3fc8f59 100644 --- a/Pod/Classes/TouchVisualizerConfig.swift +++ b/Pod/Classes/TouchVisualizerConfig.swift @@ -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 - } } \ No newline at end of file