Skip to content

Commit

Permalink
Glow performance fix
Browse files Browse the repository at this point in the history
  • Loading branch information
OskarGroth committed Apr 18, 2017
1 parent 4833990 commit 4393f3d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 45 deletions.
6 changes: 3 additions & 3 deletions OGCircularBar-Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class ViewController: NSViewController {
barView.addCircleBar(radius: 100, width: 10, color: pink.withAlphaComponent(0.1))
barView.addCircleBar(radius: 120, width: 15, color: blue.withAlphaComponent(0.1))
barView.addCircleBar(radius: 80, width: 10, color: green.withAlphaComponent(0.1))
barView.addBar(progress: 0.80, radius: 100, width: 10, color: pink, animate: true, glowColor: pink.withAlphaComponent(0.5), glowRadius: 5)
barView.addBar(progress: 0.45, radius: 120, width: 15, color: blue, animate: true, glowColor: blue.withAlphaComponent(0.5), glowRadius: 5)
barView.addBar(progress: 0.65, radius: 80, width: 10, color: green, animate: true, glowColor: green.withAlphaComponent(0.5), glowRadius: 5)
barView.addBar(progress: 0.80, radius: 100, width: 10, color: pink, animate: true, duration: 1.5, glowOpacity: 0.4, glowRadius: 8)
barView.addBar(progress: 0.45, radius: 120, width: 15, color: blue, animate: true, duration: 1.5, glowOpacity: 0.4, glowRadius: 8)
barView.addBar(progress: 0.65, radius: 80, width: 10, color: green, animate: true, duration: 1.5, glowOpacity: 0.4, glowRadius: 8)
}

override var representedObject: Any? {
Expand Down
2 changes: 1 addition & 1 deletion OGCircularBar.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = 'OGCircularBar'
s.version = '1.0'
s.version = '1.1'
s.summary = 'Circular progress bar for macOS'
s.homepage = 'https://github.com/OskarGroth/OGCircularBar'
s.license = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,11 @@
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "OGCircularBar/OGCircularBarView.swift"
timestampString = "514193270.468607"
timestampString = "514203286.75082"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "45"
endingLineNumber = "45"
landmarkName = "addBar(progress:radius:width:color:animate:glowColor:glowRadius:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "OGCircularBar/OGCircularBarView.swift"
timestampString = "514193312.915579"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "121"
endingLineNumber = "121"
startingLineNumber = "110"
endingLineNumber = "110"
landmarkName = "setProgress(_:duration:completion:)"
landmarkType = "7">
<Locations>
Expand Down
31 changes: 9 additions & 22 deletions OGCircularBar/OGCircularBarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class OGCircularBarView: NSView, Sequence {
super.awakeFromNib()
setup()
}

override public init(frame frameRect: NSRect) {
super.init(frame: frameRect)
setup()
Expand All @@ -35,28 +35,19 @@ public class OGCircularBarView: NSView, Sequence {

func setup() {
wantsLayer = true
layerUsesCoreImageFilters = true
}

public func addBar(progress: CGFloat, radius: CGFloat, width: CGFloat, color: NSColor, animate: Bool, glowColor: NSColor?, glowRadius: CGFloat) {
public func addBar(progress: CGFloat, radius: CGFloat, width: CGFloat, color: NSColor, animate: Bool, duration: CGFloat, glowOpacity: Float, glowRadius: CGFloat) {
let endAngle = 2*CGFloat.pi*progress
let barLayer = CircularBarLayer(center: center, radius: radius, width: width, startAngle: 0, endAngle: endAngle, color: color)
if let glowColor = glowColor {
let glowLayer = CircularBarLayer(center: center, radius: radius, width: width, startAngle: 0, endAngle: endAngle, color: glowColor)
let groupLayer = CALayer()
groupLayer.frame = barLayer.frame
let filter = CIFilter(name: "CIGaussianBlur")!
filter.setValue(glowRadius, forKey: kCIInputRadiusKey)
glowLayer.filters = [filter]
groupLayer.addSublayer(glowLayer)
groupLayer.addSublayer(barLayer)
barLayer.glowLayer = glowLayer
layer!.addSublayer(groupLayer)
} else {
layer?.addSublayer(barLayer)
}
barLayer.shadowColor = color.cgColor
barLayer.shadowRadius = glowRadius
barLayer.shadowOpacity = glowOpacity
barLayer.shadowOffset = NSSize.zero

layer?.addSublayer(barLayer)
bars.append(barLayer)
barLayer.setProgress(progress, duration: 1.5)
barLayer.setProgress(progress, duration: duration)
}

public func addCircleBar(radius: CGFloat, width: CGFloat, color: NSColor) {
Expand All @@ -77,7 +68,6 @@ public class OGCircularBarView: NSView, Sequence {

open class CircularBarLayer: CAShapeLayer, CALayerDelegate, CAAnimationDelegate {
var completion: ((Void) -> Void)?
var glowLayer: CircularBarLayer?

open var progress: CGFloat? {
get {
Expand Down Expand Up @@ -118,9 +108,6 @@ open class CircularBarLayer: CAShapeLayer, CALayerDelegate, CAAnimationDelegate
}

open func setProgress(_ progress: CGFloat, duration: CGFloat, completion: ((Void) -> Void)? = nil) {
if let glowLayer = glowLayer {
glowLayer.setProgress(progress, duration: duration)
}
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.fromValue = strokeEnd
animation.toValue = progress
Expand Down

0 comments on commit 4393f3d

Please sign in to comment.