From f76f186157fdc996591a39aa64b635bbd1437da7 Mon Sep 17 00:00:00 2001 From: Jordan Hipwell Date: Sun, 15 Jan 2017 20:17:13 -0700 Subject: [PATCH 1/2] Add haptic feedback when snapping to center --- Source/ColorWheel.swift | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Source/ColorWheel.swift b/Source/ColorWheel.swift index c6cff6a..522daec 100755 --- a/Source/ColorWheel.swift +++ b/Source/ColorWheel.swift @@ -31,6 +31,11 @@ class ColorWheel: UIView { // Retina scaling factor let scale: CGFloat = UIScreen.main.scale + @available(iOS 10.0, iOSApplicationExtension 10.0, *) + lazy var hapticSelectionGenerator: UISelectionFeedbackGenerator! = { + return UISelectionFeedbackGenerator() + }() + weak var delegate: ColorWheelDelegate? required init?(coder aDecoder: NSCoder) { @@ -131,9 +136,17 @@ class ColorWheel: UIView { let whiteThreshold: CGFloat = 10 var isCenter = false if (distance < whiteThreshold) { + if #available(iOS 10.0, iOSApplicationExtension 10.0, *) { + hapticSelectionGenerator.selectionChanged() + } + outputCoord.x = wheelLayerCenter.x outputCoord.y = wheelLayerCenter.y isCenter = true + } else { + if #available(iOS 10.0, iOSApplicationExtension 10.0, *) { + hapticSelectionGenerator.prepare() + } } return (outputCoord, isCenter) } From 96bfd71fde7a02a8e4440dea8bb78ac34a921b51 Mon Sep 17 00:00:00 2001 From: Jordan Hipwell Date: Sun, 15 Jan 2017 20:35:09 -0700 Subject: [PATCH 2/2] Only trigger haptic once when snapping to center --- Source/ColorWheel.swift | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/Source/ColorWheel.swift b/Source/ColorWheel.swift index 522daec..7821ce1 100755 --- a/Source/ColorWheel.swift +++ b/Source/ColorWheel.swift @@ -27,6 +27,7 @@ class ColorWheel: UIView { var indicatorCircleRadius: CGFloat = 12.0 var indicatorColor: CGColor = UIColor.lightGray.cgColor var indicatorBorderWidth: CGFloat = 2.0 + var isAtCenter = true // Retina scaling factor let scale: CGFloat = UIScreen.main.scale @@ -88,10 +89,9 @@ class ColorWheel: UIView { point = touch.location(in: self) } - let indicator = getIndicatorCoordinate(point) - point = indicator.point + updateIndicatorCoordinate(point) var color = (hue: CGFloat(0), saturation: CGFloat(0)) - if !indicator.isCenter { + if !isAtCenter { color = hueSaturationAtPoint(CGPoint(x: point.x*scale, y: point.y*scale)) } @@ -113,7 +113,7 @@ class ColorWheel: UIView { } } - func getIndicatorCoordinate(_ coord: CGPoint) -> (point: CGPoint, isCenter: Bool) { + func updateIndicatorCoordinate(_ coord: CGPoint) { // Making sure that the indicator can't get outside the Hue and Saturation wheel let dimension: CGFloat = min(wheelLayer.frame.width, wheelLayer.frame.height) @@ -134,21 +134,25 @@ class ColorWheel: UIView { // If the touch coordinate is close to center, focus it to the very center at set the color to white let whiteThreshold: CGFloat = 10 - var isCenter = false if (distance < whiteThreshold) { - if #available(iOS 10.0, iOSApplicationExtension 10.0, *) { - hapticSelectionGenerator.selectionChanged() - } - outputCoord.x = wheelLayerCenter.x outputCoord.y = wheelLayerCenter.y - isCenter = true + + if !isAtCenter { + if #available(iOS 10.0, iOSApplicationExtension 10.0, *) { + hapticSelectionGenerator.selectionChanged() + } + } + + isAtCenter = true } else { + isAtCenter = false + if #available(iOS 10.0, iOSApplicationExtension 10.0, *) { hapticSelectionGenerator.prepare() } } - return (outputCoord, isCenter) + point = outputCoord } func createColorWheel(_ size: CGSize) -> CGImage {