Skip to content

Commit d5e26b0

Browse files
committed
Add strokeStart / End property
1 parent ba49a10 commit d5e26b0

File tree

3 files changed

+25
-25
lines changed

3 files changed

+25
-25
lines changed

Example/Base.lproj/Main.storyboard

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14109" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14113" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
33
<device id="retina4_7" orientation="portrait">
44
<adaptation id="fullscreen"/>
55
</device>

Example/ViewController.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ class ViewController: UIViewController {
3939
private func configureHalfCircularProgress() {
4040
halfCircularProgress = KYCircularProgress(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height/2), showGuide: true)
4141
let center = CGPoint(x: view.frame.width / 2, y: view.frame.height / 4)
42-
halfCircularProgress.path = UIBezierPath(arcCenter: center, radius: CGFloat((halfCircularProgress.frame).width/3), startAngle: CGFloat(Double.pi), endAngle: CGFloat(0.0), clockwise: true)
42+
halfCircularProgress.path = UIBezierPath(arcCenter: center, radius: CGFloat((halfCircularProgress.frame).width/3), startAngle: 0, endAngle: CGFloat(Double.pi * 2), clockwise: true)
43+
halfCircularProgress.strokeStart = 0.5
4344
halfCircularProgress.colors = [UIColor(rgba: 0xA6E39DAA), UIColor(rgba: 0xAEC1E3AA), UIColor(rgba: 0xAEC1E3AA), UIColor(rgba: 0xF3C0ABAA)]
4445
halfCircularProgress.guideColor = UIColor(red: 0.1, green: 0.1, blue: 0.1, alpha: 0.4)
4546

Source/KYCircularProgress.swift

+22-23
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ open class KYCircularProgress: UIView {
3131
*/
3232
@IBInspectable open var progress: Double = 0.0 {
3333
didSet {
34-
let clipProgress = max( min(progress, Double(1.0)), Double(0.0) )
35-
progressView.update(progress: clipProgress)
34+
let clipProgress = max( min( progress, 1.0), 0.0 )
35+
progressView.update(progress: normalize(progress: clipProgress))
3636

3737
progressChanged?(clipProgress, self)
3838
delegate?.progressChanged(progress: clipProgress, circularProgress: self)
@@ -105,22 +105,22 @@ open class KYCircularProgress: UIView {
105105
}
106106

107107
/**
108-
Progress start angle.
108+
Progress start offset. (0.0 - 1.0)
109109
*/
110-
open var startAngle: Double = 0.0 {
110+
@IBInspectable open var strokeStart: Double = 0.0 {
111111
didSet {
112-
progressView.startAngle = startAngle
113-
guideView.startAngle = startAngle
112+
progressView.shapeLayer.strokeStart = CGFloat(max( min(strokeStart, 1.0), 0.0 ))
113+
guideView.shapeLayer.strokeStart = CGFloat(max( min(strokeStart, 1.0), 0.0 ))
114114
}
115115
}
116116

117117
/**
118-
Progress end angle.
118+
Progress end offset. (0.0 - 1.0)
119119
*/
120-
open var endAngle: Double = 0.0 {
120+
@IBInspectable open var strokeEnd: Double = 1.0 {
121121
didSet {
122-
progressView.endAngle = endAngle
123-
guideView.endAngle = endAngle
122+
progressView.shapeLayer.strokeEnd = CGFloat(max( min(strokeEnd, 1.0), 0.0 ))
123+
guideView.shapeLayer.strokeEnd = CGFloat(max( min(strokeEnd, 1.0), 0.0 ))
124124
}
125125
}
126126

@@ -175,7 +175,7 @@ open class KYCircularProgress: UIView {
175175
self.progressView.radius = self.radius
176176
guideView.shapeLayer.path = self.progressView.shapeLayer.path
177177
guideView.shapeLayer.strokeColor = self.tintColor.cgColor
178-
guideView.update(progress: 1.0)
178+
guideView.update(progress: normalize(progress: 1.0))
179179
return guideView
180180
}()
181181

@@ -234,8 +234,8 @@ open class KYCircularProgress: UIView {
234234
}
235235

236236
public func set(progress: Double, duration: Double) {
237-
let clipProgress = max( min(progress, Double(1.0)), Double(0.0) )
238-
progressView.update(progress: clipProgress, duration: duration)
237+
let clipProgress = max( min(progress, 1.0), 0.0 )
238+
progressView.update(progress: normalize(progress: clipProgress), duration: duration)
239239

240240
progressChanged?(clipProgress, self)
241241
delegate?.progressChanged(progress: clipProgress, circularProgress: self)
@@ -248,6 +248,10 @@ open class KYCircularProgress: UIView {
248248
}
249249
}
250250

251+
private func normalize(progress: Double) -> CGFloat {
252+
return CGFloat(strokeStart + progress * (strokeEnd - strokeStart))
253+
}
254+
251255
override open func layoutSubviews() {
252256
super.layoutSubviews()
253257

@@ -267,8 +271,6 @@ public protocol KYCircularProgressDelegate {
267271

268272
// MARK: - KYCircularShapeView
269273
class KYCircularShapeView: UIView {
270-
var startAngle = 0.0
271-
var endAngle = 0.0
272274
var radius = 0.0
273275
var scale: (x: CGFloat, y: CGFloat) = (1.0, 1.0)
274276

@@ -292,27 +294,24 @@ class KYCircularShapeView: UIView {
292294
override func layoutSubviews() {
293295
super.layoutSubviews()
294296

295-
if startAngle == endAngle {
296-
endAngle = startAngle + (Double.pi * 2)
297-
}
298297
shapeLayer.path = shapeLayer.path ?? layoutPath().cgPath
299298
var affineScale = CGAffineTransform(scaleX: scale.x, y: scale.y)
300299
shapeLayer.path = shapeLayer.path?.copy(using: &affineScale)
301300
}
302301

303302
private func layoutPath() -> UIBezierPath {
304303
let halfWidth = CGFloat(frame.width / 2.0)
305-
return UIBezierPath(arcCenter: CGPoint(x: halfWidth, y: halfWidth), radius: (frame.width - CGFloat(radius)) / 2, startAngle: CGFloat(startAngle), endAngle: CGFloat(endAngle), clockwise: true)
304+
return UIBezierPath(arcCenter: CGPoint(x: halfWidth, y: halfWidth), radius: (frame.width - CGFloat(radius)) / 2, startAngle: 0, endAngle: CGFloat(Double.pi * 2), clockwise: true)
306305
}
307306

308-
fileprivate func update(progress: Double) {
307+
fileprivate func update(progress: CGFloat) {
309308
CATransaction.begin()
310309
CATransaction.setValue(kCFBooleanTrue, forKey: kCATransactionDisableActions)
311-
shapeLayer.strokeEnd = CGFloat(progress)
310+
shapeLayer.strokeEnd = progress
312311
CATransaction.commit()
313312
}
314313

315-
fileprivate func update(progress: Double, duration: Double) {
314+
fileprivate func update(progress: CGFloat, duration: Double) {
316315
CATransaction.begin()
317316
let animation = CABasicAnimation(keyPath: "strokeEnd")
318317
animation.duration = duration
@@ -321,7 +320,7 @@ class KYCircularShapeView: UIView {
321320
animation.toValue = progress
322321
shapeLayer.add(animation, forKey: "animateStrokeEnd")
323322
CATransaction.commit()
324-
shapeLayer.strokeEnd = CGFloat(progress)
323+
shapeLayer.strokeEnd = progress
325324
}
326325
}
327326

0 commit comments

Comments
 (0)