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

Commit

Permalink
Second round of PR fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Metzing committed Feb 13, 2018
1 parent 1d2ea3f commit c40526c
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 106 deletions.
59 changes: 59 additions & 0 deletions SwiftMonkeyPaws/Configuration.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
53 changes: 26 additions & 27 deletions SwiftMonkeyPaws/MonkeyPaws.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand Down
62 changes: 0 additions & 62 deletions SwiftMonkeyPaws/Sources/Extensions/MonkeyPaws+Configuration.swift

This file was deleted.

24 changes: 8 additions & 16 deletions SwiftMonkeyPaws/SwiftMonkeyPaws.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<group>"; };
189807AF202338A300B9F544 /* UIColor+MonkeyPaws.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIColor+MonkeyPaws.swift"; sourceTree = "<group>"; };
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 = "<group>"; };
Expand All @@ -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 = "<group>";
};
C929B55A1DD0B7C9004B256F /* Frameworks */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -85,7 +76,6 @@
isa = PBXGroup;
children = (
OBJ_8 /* SwiftMonkeyPaws */,
189807AE202338A300B9F544 /* Extensions */,
);
path = Sources;
sourceTree = "<group>";
Expand All @@ -94,7 +84,9 @@
isa = PBXGroup;
children = (
OBJ_9 /* MonkeyPaws.swift */,
1810356E20337680005D6D35 /* Configuration.swift */,
189807B820279AD800B9F544 /* MonkeyPawDrawer.swift */,
1810356F20337681005D6D35 /* UIColor+MonkeyPaws.swift */,
);
name = SwiftMonkeyPaws;
path = .;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c40526c

Please sign in to comment.