-
-
Notifications
You must be signed in to change notification settings - Fork 197
Refactored TRCircularProgressLayer to swift #228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import UIKit | ||
|
|
||
| @objc(TRCircularProgressLayer) class CircularProgressLayer: CALayer { | ||
| @objc var progress: CGFloat = CGFloat(0) | ||
| @objc var radius = CGFloat(15.0) | ||
| @objc let outerRingWidth = CGFloat(3.0) | ||
|
|
||
| required init?(coder aDecoder: NSCoder) { | ||
| super.init(coder: aDecoder) | ||
| } | ||
|
|
||
| override init() { | ||
| super.init() | ||
| self.actions = [ | ||
| "bounds": NSNull(), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Curious, does a |
||
| "contents": NSNull(), | ||
| "position": NSNull(), | ||
| ] | ||
| self.contentsScale = UIScreen.main.scale | ||
| self.needsDisplayOnBoundsChange = true | ||
| } | ||
|
|
||
| init(layer: CircularProgressLayer) { | ||
| super.init(layer: layer) | ||
| progress = layer.progress | ||
| radius = layer.radius | ||
| } | ||
|
|
||
| override func draw(in ctx: CGContext) { | ||
| let center = CGPoint(x: self.bounds.width / 2.0, y: self.bounds.height / 2.0) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that in order to match the previous behaviour of |
||
| let progress = min(self.progress, 1.0 - CGFloat.ulpOfOne) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this could drop the explicit |
||
| let radians = (progress * .pi * 2.0) - .pi | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be |
||
|
|
||
| ctx.setFillColor(self.backgroundColor ?? UIColor.black.cgColor) | ||
| ctx.fill(bounds) | ||
| ctx.setBlendMode(.clear) | ||
| ctx.setLineWidth(CGFloat(outerRingWidth)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is |
||
| ctx.setStrokeColor(UIColor.clear.cgColor) | ||
| ctx.addArc( | ||
| center: center, | ||
| radius: CGFloat(radius), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is |
||
| startAngle: 0.0, | ||
| endAngle: 2 * .pi, | ||
| clockwise: false | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The previous version of this specified clockwise as |
||
| ) | ||
| ctx.strokePath() | ||
|
|
||
| if progress > 0.0 { | ||
| ctx.setFillColor(UIColor.clear.cgColor) | ||
| let progressPath = CGMutablePath() | ||
| progressPath.move(to: center) | ||
| progressPath.addArc( | ||
| center: center, | ||
| radius: CGFloat(self.radius), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Already |
||
| startAngle: 3.0 * .pi, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was previously |
||
| endAngle: radians, | ||
| clockwise: false | ||
| ) | ||
| ctx.closePath() | ||
| ctx.addPath(progressPath) | ||
| ctx.fillPath() | ||
| } | ||
| } | ||
|
|
||
| override class func needsDisplay(forKey key: String) -> Bool { | ||
| switch key { | ||
| case "progress", "radius", "outerRingWidth": | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could use |
||
| return true | ||
| default: | ||
| return false | ||
| } | ||
| } | ||
| } | ||
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be either
: CGFloat = 0or= CGFloat(0)(I usually prefer the former).