From c40526c46f0bfbc9bdf1df589b2a9688297896de Mon Sep 17 00:00:00 2001 From: Daniel Metzing Date: Tue, 13 Feb 2018 20:49:09 +0100 Subject: [PATCH] Second round of PR fixes --- SwiftMonkeyPaws/Configuration.swift | 59 ++++++++++++++++++ SwiftMonkeyPaws/MonkeyPaws.swift | 53 ++++++++-------- .../Extensions/MonkeyPaws+Configuration.swift | 62 ------------------- .../SwiftMonkeyPaws.xcodeproj/project.pbxproj | 24 +++---- .../Extensions => }/UIColor+MonkeyPaws.swift | 2 +- 5 files changed, 94 insertions(+), 106 deletions(-) create mode 100644 SwiftMonkeyPaws/Configuration.swift delete mode 100644 SwiftMonkeyPaws/Sources/Extensions/MonkeyPaws+Configuration.swift rename SwiftMonkeyPaws/{Sources/Extensions => }/UIColor+MonkeyPaws.swift (88%) diff --git a/SwiftMonkeyPaws/Configuration.swift b/SwiftMonkeyPaws/Configuration.swift new file mode 100644 index 0000000..036529c --- /dev/null +++ b/SwiftMonkeyPaws/Configuration.swift @@ -0,0 +1,59 @@ +// +// Configuration.swift +// SwiftMonkeyPaws +// +// Created by Daniel.Metzing on 11.02.18. +// + +import UIKit + +public struct Configuration { + // Customise the appearance of the paws + public struct Paws { + + /// Define the colour of the Paws + /// + /// - randomized: random colour for each paw + /// - constant: same colour for the paws + public enum Color { + case randomized + case constant(UIColor) + } + // Colour of the paws + public let color: Color + + // Brightness of a particular paw + public let brightness: CGFloat + + // Maximum visible paws at one time + public let maxShown: Int + + public init(colour: Color = .randomized, brightness: CGFloat = 0.5, maxShown: Int = 15) { + self.color = colour + self.brightness = brightness + self.maxShown = maxShown + } + } + + public struct Radius { + + /// Radius of the cross draw upon canceling a touch event + public let cross: CGFloat + + /// Radius of the circle draw upon ending a touch event + public let circle: CGFloat + + public init(cross: CGFloat = 7, circle: CGFloat = 7) { + self.cross = cross + self.circle = circle + } + } + + public let paws: Paws + public let radius: Radius + + public init(paws: Paws = Paws(), radius: Radius = Radius()) { + self.paws = paws + self.radius = radius + } +} diff --git a/SwiftMonkeyPaws/MonkeyPaws.swift b/SwiftMonkeyPaws/MonkeyPaws.swift index 13dbf66..203a7d5 100644 --- a/SwiftMonkeyPaws/MonkeyPaws.swift +++ b/SwiftMonkeyPaws/MonkeyPaws.swift @@ -39,7 +39,7 @@ public class MonkeyPaws: NSObject, CALayerDelegate { private var gestures: [(hash: Int?, gesture: Gesture)] = [] private weak var view: UIView? - let configuration: MonkeyPaws.Configuration + let configuration: Configuration let bezierPathDrawer: BezierPathDrawer let layer = CALayer() @@ -124,11 +124,11 @@ public class MonkeyPaws: NSObject, CALayerDelegate { gesture.extend(to: point) } } else { - if gestures.count > self.configuration.paws.maxShown { + if gestures.count > configuration.paws.maxShown { gestures.removeFirst() } - gestures.append((hash: touchHash, gesture: Gesture(from: point, inLayer: layer, configuration: self.configuration, bezierPathDrawer: self.bezierPathDrawer))) + gestures.append((hash: touchHash, gesture: Gesture(from: point, inLayer: layer, configuration: configuration, bezierPathDrawer: bezierPathDrawer))) for i in 0 ..< gestures.count { let number = gestures.count - i @@ -187,33 +187,20 @@ private class Gesture { var pathLayer: CAShapeLayer? var endLayer: CAShapeLayer? - let configuration: MonkeyPaws.Configuration - let bezierPathDrawer: MonkeyPaws.BezierPathDrawer + let configuration: Configuration private static var counter: Int = 0 - init(from: CGPoint, inLayer: CALayer, configuration: MonkeyPaws.Configuration, bezierPathDrawer: @escaping MonkeyPaws.BezierPathDrawer) { + init(from: CGPoint, inLayer: CALayer, configuration: Configuration, bezierPathDrawer: @escaping MonkeyPaws.BezierPathDrawer) { self.points = [from] self.configuration = configuration - self.bezierPathDrawer = bezierPathDrawer let counter = Gesture.counter Gesture.counter += 1 - let angle = 45 * (CGFloat(fmod(Float(counter) * 0.279, 1)) * 2 - 1) - let mirrored = counter % 2 == 0 - let colour: UIColor - switch configuration.paws.colour { - case .randomized: - colour = UIColor(hue: CGFloat(fmod(Float(counter) * 0.391, 1)), - saturation: 1, - brightness: self.configuration.paws.brightness, - alpha: 1) - case .constant(let constantColour): - colour = constantColour.colorWithBrightness(brightness: self.configuration.paws.brightness) - } + let colour: UIColor = pawsColor(configuration: configuration.paws, seed: counter) - startLayer.path = customizePath(self.bezierPathDrawer(), angle: angle, scale: 1, mirrored: mirrored).cgPath + startLayer.path = customize(path: bezierPathDrawer(), seed: counter).cgPath startLayer.strokeColor = colour.cgColor startLayer.fillColor = nil @@ -241,9 +228,9 @@ private class Gesture { didSet { numberLayer.string = String(number) - let fraction = Float(number - 1) / Float(self.configuration.paws.maxShown) + let fraction = Float(number - 1) / Float(configuration.paws.maxShown) let alpha = sqrt(1 - fraction) - self.containerLayer.opacity = alpha + containerLayer.opacity = alpha } } @@ -299,7 +286,7 @@ private class Gesture { layer.fillColor = nil layer.position = at - let path = circlePath(radius: self.configuration.radius.circle) + let path = circlePath(radius: configuration.radius.circle) layer.path = path.cgPath containerLayer.addSublayer(layer) @@ -319,18 +306,30 @@ private class Gesture { layer.fillColor = nil layer.position = at - let path = crossPath(radius: self.configuration.radius.cross) + let path = crossPath(radius: configuration.radius.cross) layer.path = path.cgPath containerLayer.addSublayer(layer) endLayer = layer } + + func pawsColor(configuration: Configuration.Paws, seed: Int) -> UIColor { + switch configuration.color { + case .randomized: + return UIColor(hue: CGFloat(fmod(Float(seed) * 0.391, 1)), + saturation: 1, + brightness: configuration.brightness, + alpha: 1) + case .constant(let constantColour): + return constantColour.color(WithBrightness: configuration.brightness) + } + } } -private func customizePath(_ path: UIBezierPath, angle: CGFloat, scale: CGFloat, mirrored: Bool) -> UIBezierPath { - path.apply(CGAffineTransform(translationX: 0.5, y: 0)) +private func customize(path: UIBezierPath, seed: Int) -> UIBezierPath { - path.apply(CGAffineTransform(scaleX: scale, y: scale)) + let angle = 45 * (CGFloat(fmod(Float(seed) * 0.279, 1)) * 2 - 1) + let mirrored = seed % 2 == 0 if mirrored { path.apply(CGAffineTransform(scaleX: -1, y: 1)) diff --git a/SwiftMonkeyPaws/Sources/Extensions/MonkeyPaws+Configuration.swift b/SwiftMonkeyPaws/Sources/Extensions/MonkeyPaws+Configuration.swift deleted file mode 100644 index 3b66558..0000000 --- a/SwiftMonkeyPaws/Sources/Extensions/MonkeyPaws+Configuration.swift +++ /dev/null @@ -1,62 +0,0 @@ -// -// MonkeyPaws+Configuration.swift -// SwiftMonkeyPaws -// -// Created by Daniel.Metzing on 11.02.18. -// - -import UIKit - -extension MonkeyPaws { - - public struct Configuration { - // Customise the appearance of the paws - public struct Paws { - - /// Define the colour of the Paws - /// - /// - randomized: random colour for each paw - /// - constant: same colour for the paws - public enum Colour { - case randomized - case constant(UIColor) - } - // Colour of the paws - public let colour: Colour - - // Brightness of a particular paw - public let brightness: CGFloat - - // Maximum visible paws at one time - public let maxShown: Int - - public init(colour: Colour = .randomized, brightness: CGFloat = 0.5, maxShown: Int = 15) { - self.colour = colour - self.brightness = brightness - self.maxShown = maxShown - } - } - - public struct Radius { - - /// Radius of the cross draw upon canceling a touch event - public let cross: CGFloat - - /// Radius of the circle draw upon ending a touch event - public let circle: CGFloat - - public init(cross: CGFloat = 7, circle: CGFloat = 7) { - self.cross = cross - self.circle = circle - } - } - - public let paws: Paws - public let radius: Radius - - public init(paws: Paws = Paws(), radius: Radius = Radius()) { - self.paws = paws - self.radius = radius - } - } -} diff --git a/SwiftMonkeyPaws/SwiftMonkeyPaws.xcodeproj/project.pbxproj b/SwiftMonkeyPaws/SwiftMonkeyPaws.xcodeproj/project.pbxproj index f878f02..df432d2 100644 --- a/SwiftMonkeyPaws/SwiftMonkeyPaws.xcodeproj/project.pbxproj +++ b/SwiftMonkeyPaws/SwiftMonkeyPaws.xcodeproj/project.pbxproj @@ -7,16 +7,16 @@ objects = { /* Begin PBXBuildFile section */ - 183234262030AF3A00B4C6DD /* MonkeyPaws+Configuration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 183234252030AF3A00B4C6DD /* MonkeyPaws+Configuration.swift */; }; - 189807B0202338A300B9F544 /* UIColor+MonkeyPaws.swift in Sources */ = {isa = PBXBuildFile; fileRef = 189807AF202338A300B9F544 /* UIColor+MonkeyPaws.swift */; }; + 1810357020337681005D6D35 /* Configuration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1810356E20337680005D6D35 /* Configuration.swift */; }; + 1810357120337681005D6D35 /* UIColor+MonkeyPaws.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1810356F20337681005D6D35 /* UIColor+MonkeyPaws.swift */; }; 189807B920279AD800B9F544 /* MonkeyPawDrawer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 189807B820279AD800B9F544 /* MonkeyPawDrawer.swift */; }; C929B55C1DD0B7C9004B256F /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C929B55B1DD0B7C9004B256F /* UIKit.framework */; }; OBJ_18 /* MonkeyPaws.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_9 /* MonkeyPaws.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ - 183234252030AF3A00B4C6DD /* MonkeyPaws+Configuration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MonkeyPaws+Configuration.swift"; sourceTree = ""; }; - 189807AF202338A300B9F544 /* UIColor+MonkeyPaws.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIColor+MonkeyPaws.swift"; sourceTree = ""; }; + 1810356E20337680005D6D35 /* Configuration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Configuration.swift; sourceTree = SOURCE_ROOT; }; + 1810356F20337681005D6D35 /* UIColor+MonkeyPaws.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIColor+MonkeyPaws.swift"; sourceTree = SOURCE_ROOT; }; 189807B820279AD800B9F544 /* MonkeyPawDrawer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MonkeyPawDrawer.swift; sourceTree = SOURCE_ROOT; }; C929B55B1DD0B7C9004B256F /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; }; C943263D1DDB123A0038A891 /* SwiftMonkeyPaws.podspec */ = {isa = PBXFileReference; lastKnownFileType = text; name = SwiftMonkeyPaws.podspec; path = ../SwiftMonkeyPaws.podspec; sourceTree = ""; }; @@ -37,15 +37,6 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ - 189807AE202338A300B9F544 /* Extensions */ = { - isa = PBXGroup; - children = ( - 189807AF202338A300B9F544 /* UIColor+MonkeyPaws.swift */, - 183234252030AF3A00B4C6DD /* MonkeyPaws+Configuration.swift */, - ); - path = Extensions; - sourceTree = ""; - }; C929B55A1DD0B7C9004B256F /* Frameworks */ = { isa = PBXGroup; children = ( @@ -85,7 +76,6 @@ isa = PBXGroup; children = ( OBJ_8 /* SwiftMonkeyPaws */, - 189807AE202338A300B9F544 /* Extensions */, ); path = Sources; sourceTree = ""; @@ -94,7 +84,9 @@ isa = PBXGroup; children = ( OBJ_9 /* MonkeyPaws.swift */, + 1810356E20337680005D6D35 /* Configuration.swift */, 189807B820279AD800B9F544 /* MonkeyPawDrawer.swift */, + 1810356F20337681005D6D35 /* UIColor+MonkeyPaws.swift */, ); name = SwiftMonkeyPaws; path = .; @@ -155,8 +147,8 @@ buildActionMask = 0; files = ( OBJ_18 /* MonkeyPaws.swift in Sources */, - 189807B0202338A300B9F544 /* UIColor+MonkeyPaws.swift in Sources */, - 183234262030AF3A00B4C6DD /* MonkeyPaws+Configuration.swift in Sources */, + 1810357020337681005D6D35 /* Configuration.swift in Sources */, + 1810357120337681005D6D35 /* UIColor+MonkeyPaws.swift in Sources */, 189807B920279AD800B9F544 /* MonkeyPawDrawer.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/SwiftMonkeyPaws/Sources/Extensions/UIColor+MonkeyPaws.swift b/SwiftMonkeyPaws/UIColor+MonkeyPaws.swift similarity index 88% rename from SwiftMonkeyPaws/Sources/Extensions/UIColor+MonkeyPaws.swift rename to SwiftMonkeyPaws/UIColor+MonkeyPaws.swift index 6d6538f..c8c0afd 100644 --- a/SwiftMonkeyPaws/Sources/Extensions/UIColor+MonkeyPaws.swift +++ b/SwiftMonkeyPaws/UIColor+MonkeyPaws.swift @@ -8,7 +8,7 @@ import UIKit extension UIColor { - func colorWithBrightness(brightness: CGFloat) -> UIColor { + func color(WithBrightness brightness: CGFloat) -> UIColor { var H: CGFloat = 0 var S: CGFloat = 0 var B: CGFloat = 0