Skip to content

Commit

Permalink
fixed a bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryu0118 committed Jun 13, 2022
1 parent 773d53b commit 206cf6b
Showing 1 changed file with 52 additions and 53 deletions.
105 changes: 52 additions & 53 deletions Sources/SRCircleProgress/SRCircleProgress.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import UIKit
open class SRCircleProgress: UIView {
/**
ProgressAngle is an enumerated type that specifies the angle at which the progress view begins and ends.


To customize the angle more, assign arbitrary values to the startAngle and endAngle properties of SRCircleProgress
*/
public enum ProgressAngle {
case topToTop, bottomToBottom, lowerLeftToLowerRight, leftToRight

fileprivate var startAngle: CGFloat {
switch self {
case .topToTop:
Expand All @@ -24,7 +24,7 @@ open class SRCircleProgress: UIView {
return CGFloat(Double.pi)
}
}

fileprivate var endAngle: CGFloat {
switch self {
case .topToTop:
Expand All @@ -38,20 +38,16 @@ open class SRCircleProgress: UIView {
}
}
}

// MARK: Public Properties
/**
The current progress shown by the receiver.

The current progress is represented by a floating-point value between 0.0 and 1.0, inclusive, where 1.0 indicates the completion of the task. The default value is 0.0. Values less than 0.0 and greater than 1.0 are pinned to those limits.
*/
open var progress: Float {
get { Float(progressLineLayer.strokeEnd) }
set {
disableAnimation {
progressLineLayer.strokeEnd = CGFloat(newValue)
}
}
set { setProgress(newValue, animated: false) }
}

/**
Expand All @@ -65,11 +61,11 @@ open class SRCircleProgress: UIView {
get { Int(progress * 100) }
set { progress = Float(newValue) / 100 }
}

/**
The color shown for the portion of the progress view that is filled.


The default is UIColor.systemBlue
*/
@IBInspectable
Expand All @@ -78,10 +74,10 @@ open class SRCircleProgress: UIView {
progressLineLayer.strokeColor = progressLineColor.cgColor
}
}

/**
Color of the view displayed behind the progress view

The default is UIColor.systemBackground
*/
@IBInspectable
Expand All @@ -90,11 +86,11 @@ open class SRCircleProgress: UIView {
backgroundLineLayer.strokeColor = backgroundLineColor.cgColor
}
}

/**
Specifies the line width of the progress view.


The default is 6.
*/
@IBInspectable
Expand All @@ -103,11 +99,11 @@ open class SRCircleProgress: UIView {
progressLineLayer.lineWidth = CGFloat(progressLineWidth)
}
}

/**
Specifies the line width of the view behind the progress view.


The default is 12.
*/
@IBInspectable
Expand All @@ -116,43 +112,43 @@ open class SRCircleProgress: UIView {
backgroundLineLayer.lineWidth = CGFloat(backgroundLineWidth)
}
}

/**
Specifies the duration of the animation when setProgress(_:animated:) is executed
*/
public var animationDuration: TimeInterval = 0.2

/**
Specifies the angle at which the progress view begins and ends.


To customize the angle more, assign arbitrary values to the startAngle and endAngle properties of SRCircleProgress
*/
public var progressAngle: ProgressAngle? {
didSet {
guard let progressAngle = progressAngle else { return }

self.startAngle = progressAngle.startAngle
self.endAngle = progressAngle.endAngle
}
}

/**
Specifies the angle at which the progress view starts


To more easily specify the beginning and end angles of a progress view, use the progressAngle property
*/
public var startAngle = CGFloat(-Double.pi / 2)

/**
Specifies the angle at which the progress view ends


To more easily specify the beginning and end angles of a progress view, use the progressAngle property
*/
public var endAngle = CGFloat(3 * Double.pi / 2)

// MARK: Private Properties
private let backgroundLineLayer = CAShapeLayer()

Expand All @@ -168,27 +164,27 @@ open class SRCircleProgress: UIView {
clockwise: true
)
}

override public init(frame: CGRect) {
public override init(frame: CGRect) {
super.init(frame: frame)
addLayer()
}

public required init?(coder: NSCoder) {
super.init(coder: coder)
addLayer()
}

override public func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
setup()
}

override open func layoutSubviews() {
super.layoutSubviews()
setup()
}

/// Adjusts the current progress shown by the receiver, optionally animating the change.
///
///
Expand All @@ -205,14 +201,12 @@ open class SRCircleProgress: UIView {
progressAnimation.toValue = CGFloat(progress)
progressAnimation.isRemovedOnCompletion = false
progressLineLayer.add(progressAnimation, forKey: "progress")
} else {
self.progress = progress
}
}

private func addLayer() {
layer.addSublayer(backgroundLineLayer)
layer.addSublayer(progressLineLayer)
else {
disableAnimation {
progressLineLayer.strokeEnd = CGFloat(progress)
}
}
}

private func disableAnimation(_ block: () -> ()) {
Expand All @@ -221,25 +215,30 @@ open class SRCircleProgress: UIView {
block()
CATransaction.commit()
}


private func addLayer() {
backgroundLineLayer.strokeEnd = 1.0
progressLineLayer.strokeEnd = 0.0
layer.addSublayer(backgroundLineLayer)
layer.addSublayer(progressLineLayer)
}

private func setup() {
let circlePath = self.circlePath

backgroundLineLayer.lineCap = .round
backgroundLineLayer.path = circlePath.cgPath
backgroundLineLayer.fillColor = UIColor.clear.cgColor
backgroundLineLayer.lineWidth = CGFloat(backgroundLineWidth)
backgroundLineLayer.strokeColor = backgroundLineColor.cgColor
backgroundLineLayer.strokeEnd = 1.0


progressLineLayer.lineCap = .round
progressLineLayer.path = circlePath.cgPath
progressLineLayer.fillColor = UIColor.clear.cgColor
progressLineLayer.lineWidth = CGFloat(progressLineWidth)
progressLineLayer.strokeEnd = 0
progressLineLayer.strokeColor = progressLineColor.cgColor
}

}

#endif

0 comments on commit 206cf6b

Please sign in to comment.