From a74d6235d2118cfc1ab334877689f75941eb7f80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Blagaji=C4=87?= Date: Mon, 25 Mar 2019 15:58:24 +0100 Subject: [PATCH] Optionally rotate thumb when sliding --- .../Base.lproj/Main.storyboard | 29 ++++++++---------- .../AppIcon.appiconset/Contents.json | 5 +++ .../arrow_thumb.imageset/Contents.json | 21 +++++++++++++ .../arrow_thumb.imageset/arrow_thumb.png | Bin 0 -> 1607 bytes .../Classes/CircularSlider+Draw.swift | 11 ++++++- HGCircularSlider/Classes/CircularSlider.swift | 13 +++++++- .../Classes/MidPointCircularSlider.swift | 2 +- .../Classes/RangeCircularSlider.swift | 4 +-- 8 files changed, 64 insertions(+), 21 deletions(-) create mode 100644 Example/HGCircularSlider/Images.xcassets/arrow_thumb.imageset/Contents.json create mode 100644 Example/HGCircularSlider/Images.xcassets/arrow_thumb.imageset/arrow_thumb.png diff --git a/Example/HGCircularSlider/Base.lproj/Main.storyboard b/Example/HGCircularSlider/Base.lproj/Main.storyboard index 481e1b0..55b8523 100755 --- a/Example/HGCircularSlider/Base.lproj/Main.storyboard +++ b/Example/HGCircularSlider/Base.lproj/Main.storyboard @@ -1,12 +1,11 @@ - + - - - + + @@ -154,7 +153,7 @@ - + @@ -177,7 +176,7 @@ - + @@ -290,7 +289,7 @@ - + @@ -382,7 +381,7 @@ - + @@ -584,7 +583,7 @@ - + @@ -602,7 +601,7 @@ + - - - - - diff --git a/Example/HGCircularSlider/Images.xcassets/AppIcon.appiconset/Contents.json b/Example/HGCircularSlider/Images.xcassets/AppIcon.appiconset/Contents.json index b8236c6..19882d5 100755 --- a/Example/HGCircularSlider/Images.xcassets/AppIcon.appiconset/Contents.json +++ b/Example/HGCircularSlider/Images.xcassets/AppIcon.appiconset/Contents.json @@ -39,6 +39,11 @@ "idiom" : "iphone", "size" : "60x60", "scale" : "3x" + }, + { + "idiom" : "ios-marketing", + "size" : "1024x1024", + "scale" : "1x" } ], "info" : { diff --git a/Example/HGCircularSlider/Images.xcassets/arrow_thumb.imageset/Contents.json b/Example/HGCircularSlider/Images.xcassets/arrow_thumb.imageset/Contents.json new file mode 100644 index 0000000..37dddad --- /dev/null +++ b/Example/HGCircularSlider/Images.xcassets/arrow_thumb.imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "idiom" : "universal", + "filename" : "arrow_thumb.png", + "scale" : "1x" + }, + { + "idiom" : "universal", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/Example/HGCircularSlider/Images.xcassets/arrow_thumb.imageset/arrow_thumb.png b/Example/HGCircularSlider/Images.xcassets/arrow_thumb.imageset/arrow_thumb.png new file mode 100644 index 0000000000000000000000000000000000000000..54f32278491c45e57f4047190ae3a84989020fe6 GIT binary patch literal 1607 zcmeAS@N?(olHy`uVBq!ia0vp^1|ZDA1|-9oezpTC#^NA%Cx&(BWL^R}oCO|{#S9F5 zM?jcysy3fAP(fs7NJL45ua8x7ey(0(N`6wRUPW#JP&EUCO@$SZnVVXYs8ErclUHn2 zVXFjIVFhG^g!Ppaz)DK8ZIvQ?0~DO|i&7O#^-S~(l1AfjnEK zjFOT9D}DX)@^Za$W4-*MbbUihOG|wNBYh(yU7!lx;>x^|#0uTKVr7USFmqf|i<65o z3raHc^AtelCMM;Vme?vOfh>Xph&xL%(-1c06+^uR^q@XSM&D4+Kp$>4P^%3{)XKjo zGZknv$b36P8?Z_gF{nK@`XI}Z90TzwSQO}0J1!f2c(B=V`5aP@WME)v_jGX#$zZ)2 z<(n<+C^4@*$>+?@ihzv^GhT~7a+JUTd8WRFG@`(6%K!N1(s zjyc&@2ye_x6E<(~+%T zUiN=ohvvd7s;-Y*)WbYF`I}z+T(@S%&x%=fr76nmE?vC-XWPl$i#wJ@m`jK6Z>?#a zGVA8bCwq?;f2=yXll`1nXxUo5yvikc>Ku2T1W)W*_I=5bzuQ??&FBp*R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ift index 29fe75d..d1772af 100644 --- a/HGCircularSlider/Classes/CircularSlider+Draw.swift +++ b/HGCircularSlider/Classes/CircularSlider+Draw.swift @@ -125,14 +125,23 @@ extension CircularSlider { - returns: return the origin point of the thumb */ @discardableResult - internal func drawThumb(withImage image: UIImage, angle: CGFloat, inContext context: CGContext) -> CGPoint { + internal func drawThumb(withImage image: UIImage, angle: CGFloat, inContext context: CGContext, rotate: Bool) -> CGPoint { UIGraphicsPushContext(context) context.beginPath() let circle = Circle(origin: bounds.center, radius: self.radius) let thumbOrigin = CircularSliderHelper.endPoint(fromCircle: circle, angle: angle) let imageSize = image.size let imageFrame = CGRect(x: thumbOrigin.x - (imageSize.width / 2), y: thumbOrigin.y - (imageSize.height / 2), width: imageSize.width, height: imageSize.height) + if rotate { + context.saveGState() + context.translateBy(x: imageFrame.center.x, y: imageFrame.center.y) + context.rotate(by: angle) + context.translateBy(x: -imageFrame.center.x, y: -imageFrame.center.y) + } image.draw(in: imageFrame) + if rotate { + context.restoreGState() + } UIGraphicsPopContext() return thumbOrigin diff --git a/HGCircularSlider/Classes/CircularSlider.swift b/HGCircularSlider/Classes/CircularSlider.swift index ca90b5a..5476398 100644 --- a/HGCircularSlider/Classes/CircularSlider.swift +++ b/HGCircularSlider/Classes/CircularSlider.swift @@ -126,6 +126,7 @@ open class CircularSlider: UIControl { * * The default value of this property is nil */ + @IBInspectable open var endThumbImage: UIImage? // MARK: Accessing the Slider’s Value Limits @@ -172,6 +173,16 @@ open class CircularSlider: UIControl { } } + /** + * When set to true, thumb image will rotate when the value changes. + */ + @IBInspectable + open var rotatesThumb: Bool = false { + didSet { + setNeedsDisplay() + } + } + /** * The value of the endThumb (changed when the user change the position of the end thumb) * @@ -265,7 +276,7 @@ open class CircularSlider: UIControl { drawThumb(withAngle: endAngle, inContext: context) return } - drawThumb(withImage: image, angle: endAngle, inContext: context) + drawThumb(withImage: image, angle: endAngle, inContext: context, rotate: rotatesThumb) } // MARK: User interaction methods diff --git a/HGCircularSlider/Classes/MidPointCircularSlider.swift b/HGCircularSlider/Classes/MidPointCircularSlider.swift index f30c2e6..96f80c6 100755 --- a/HGCircularSlider/Classes/MidPointCircularSlider.swift +++ b/HGCircularSlider/Classes/MidPointCircularSlider.swift @@ -121,7 +121,7 @@ open class MidPointCircularSlider: RangeCircularSlider { drawThumb(withAngle: midAngle, inContext: context) return } - drawThumb(withImage: image, angle: midAngle, inContext: context) + drawThumb(withImage: image, angle: midAngle, inContext: context, rotate: rotatesThumb) } // MARK: User interaction methods diff --git a/HGCircularSlider/Classes/RangeCircularSlider.swift b/HGCircularSlider/Classes/RangeCircularSlider.swift index e2a31f6..94621c8 100644 --- a/HGCircularSlider/Classes/RangeCircularSlider.swift +++ b/HGCircularSlider/Classes/RangeCircularSlider.swift @@ -237,7 +237,7 @@ open class RangeCircularSlider: CircularSlider { (isHighlighted == true && selectedThumb == .endThumb) ? endThumbStrokeHighlightedColor.setStroke() : endThumbStrokeColor.setStroke() endThumbCenter = drawThumb(withAngle: endAngle, inContext: context) if let image = endThumbImage { - endThumbCenter = drawThumb(withImage: image, angle: endAngle, inContext: context) + endThumbCenter = drawThumb(withImage: image, angle: endAngle, inContext: context, rotate: rotatesThumb) } // start thumb @@ -246,7 +246,7 @@ open class RangeCircularSlider: CircularSlider { startThumbCenter = drawThumb(withAngle: startAngle, inContext: context) if let image = startThumbImage { - startThumbCenter = drawThumb(withImage: image, angle: startAngle, inContext: context) + startThumbCenter = drawThumb(withImage: image, angle: startAngle, inContext: context, rotate: rotatesThumb) } }