diff --git a/Sources/ShowTime/ShowTime.swift b/Sources/ShowTime/ShowTime.swift index d74fbd6..ad77c05 100644 --- a/Sources/ShowTime/ShowTime.swift +++ b/Sources/ShowTime/ShowTime.swift @@ -32,10 +32,10 @@ public final class ShowTime: NSObject { } /// Whether ShowTime is enabled. - /// ShowTime automatically enables itself by default. - /// (`.always` by default) - @objc public static var enabled: Enabled = .always - + /// ShowTime automatically disabled by default. + /// (`.never` by default) + @objc public static var enabled: Enabled = .never + /// The fill (background) colour of the visual touches. /// If set to `.auto`, ShowTime automatically uses the stroke color with 50% alpha. /// (`.auto` by default) @@ -49,10 +49,10 @@ public final class ShowTime: NSObject { /// (3pt by default) @objc public static var strokeWidth: CGFloat = 3 - /// The size of the touch circles. + /// The diametr of the touch circles. /// (44pt x 44pt by default) - @objc public static var size = CGSize(width: 44, height: 44) // TODO: Just make CGFloat - + @objc public static var size: CGFloat = 44 + /// The style of animation to use when hiding a visual touch. /// (`.standard` by default) public static var disappearAnimation: Animation = .standard @@ -112,10 +112,10 @@ class TouchView: UILabel { /// - view: A view the touch is relative to, typically the window calling `sendEvent(_:)`. convenience init(touch: UITouch, relativeTo view: UIView) { let location = touch.location(in: view) - self.init(frame: CGRect(x: location.x - ShowTime.size.width / 2, - y: location.y - ShowTime.size.height / 2, - width: ShowTime.size.width, - height: ShowTime.size.height)) + self.init(frame: CGRect(x: location.x - ShowTime.size / 2, + y: location.y - ShowTime.size / 2, + width: ShowTime.size, + height: ShowTime.size)) style(with: touch) } @@ -126,7 +126,7 @@ class TouchView: UILabel { /// - view: A view the touch is relative to, typically the window calling `sendEvent(_:)`. func update(with touch: UITouch, relativeTo view: UIView) { let location = touch.location(in: view) - frame = CGRect(x: location.x - ShowTime.size.width / 2, y: location.y - ShowTime.size.height / 2, width: ShowTime.size.width, height: ShowTime.size.height) + frame = CGRect(x: location.x - ShowTime.size / 2, y: location.y - ShowTime.size / 2, width: ShowTime.size, height: ShowTime.size) if ShowTime.shouldShowForce { let scale = 1 + (0.5 * touch.normalizedForce) CATransaction.begin() @@ -167,7 +167,7 @@ class TouchView: UILabel { } private func style(with touch: UITouch) { - layer.cornerRadius = ShowTime.size.height / 2 + layer.cornerRadius = ShowTime.size / 2 layer.borderColor = ShowTime.strokeColor.cgColor layer.borderWidth = ShowTime.strokeWidth backgroundColor = ShowTime.fillColor == .auto ? ShowTime.strokeColor.withAlphaComponent(0.5) : ShowTime.fillColor @@ -185,13 +185,18 @@ var _touches = [UITouch : TouchView]() extension UIWindow { + struct Swizzled { static var once = false } // Workaround for missing dispatch_once in Swift 3 + open override var layer: CALayer { - UIWindow.swizzle() // TODO: Only swizzle when enabled + if ShowTime.shouldEnable { + UIWindow.swizzle() + } else { + UIWindow.unswizzle() + } return super.layer } private class func swizzle() { // `initialize()` removed in Swift 4 - struct Swizzled { static var once = false } // Workaround for missing dispatch_once in Swift 3 guard !Swizzled.once else { return } Swizzled.once = true guard let original = class_getInstanceMethod(self, #selector(UIWindow.sendEvent(_:))) else { return } @@ -199,6 +204,14 @@ extension UIWindow { method_exchangeImplementations(original, new) } + private class func unswizzle() { + guard Swizzled.once else { return } + Swizzled.once = false + guard let original = class_getInstanceMethod(self, #selector(UIWindow.sendEvent(_:))) else { return } + guard let new = class_getInstanceMethod(self, #selector(UIWindow.swizzled_sendEvent(_:))) else { return } + method_exchangeImplementations(new, original) + } + @objc private func swizzled_sendEvent(_ event: UIEvent) { swizzled_sendEvent(event) guard ShowTime.shouldEnable else { return removeAllTouchViews() }