Skip to content

Instantly share code, notes, and snippets.

@odrobnik
Created July 13, 2015 14:58
Show Gist options
  • Save odrobnik/bc5975b9fad0232c6775 to your computer and use it in GitHub Desktop.
Save odrobnik/bc5975b9fad0232c6775 to your computer and use it in GitHub Desktop.
import UIKit
import Darwin
@IBDesignable
class DTWheelChart: UIView {
@IBInspectable var days: Int = 28
var shapeLayers : [CAShapeLayer]!
override func layoutSubviews() {
super.layoutSubviews()
if (shapeLayers == nil)
{
shapeLayers = []
let lineWidth : CGFloat = 10
let center = CGPoint(x: self.bounds.size.width/2.0, y: self.bounds.size.height/2.0)
let radius = self.bounds.size.width/2.0 - lineWidth/2.0
let startAngle : CGFloat = 0
let endAngle : CGFloat = 0
/*
var path = UIBezierPath(arcCenter: center, radius: radius, startAngle: startAngle-CGFloat(M_PI_2), endAngle: endAngle-CGFloat(M_PI_2), clockwise: true)
var shapeLayer = CAShapeLayer()
shapeLayer.lineWidth = lineWidth
shapeLayer.strokeColor = UIColor.redColor().CGColor
shapeLayer.fillColor = nil
shapeLayer.path = path.CGPath
shapeLayer.lineCap = kCALineCapRound
self.shapeLayers.append(shapeLayer)
self.layer.addSublayer(shapeLayer)
*/
// construct compound ring bezier path
let outerRadius = self.bounds.size.width/2.0;
let innerRadius = outerRadius - lineWidth
let middleRadius = (outerRadius + innerRadius)/2.0;
var compoundPath = UIBezierPath()
// start cap
let middleStartPoint = self.positionAtAngle(center, angle: startAngle, radius: middleRadius)
compoundPath.addArcWithCenter(middleStartPoint, radius: lineWidth/2.0, startAngle: startAngle - CGFloat(M_PI_2) + CGFloat(M_PI), endAngle: startAngle - CGFloat(M_PI_2), clockwise: true)
// outer arc
compoundPath.addArcWithCenter(center, radius: outerRadius, startAngle: startAngle-CGFloat(M_PI_2), endAngle: endAngle-CGFloat(M_PI_2), clockwise: true)
// end cap
let middleEndPoint = self.positionAtAngle(center, angle: endAngle, radius: middleRadius)
compoundPath.addArcWithCenter(middleEndPoint, radius: lineWidth/2.0, startAngle: endAngle-CGFloat(M_PI_2), endAngle: endAngle+CGFloat(M_PI_2), clockwise: true)
// inner arc
compoundPath.addArcWithCenter(center, radius: innerRadius, startAngle: endAngle-CGFloat(M_PI_2), endAngle: startAngle-CGFloat(M_PI_2), clockwise: false)
var compoundShapeLayer = CAShapeLayer()
compoundShapeLayer.lineWidth = 1
compoundShapeLayer.strokeColor = UIColor.blackColor().CGColor
compoundShapeLayer.fillColor = UIColor.greenColor().CGColor
compoundShapeLayer.path = compoundPath.CGPath
self.shapeLayers.append(compoundShapeLayer)
self.layer.addSublayer(compoundShapeLayer)
}
}
func positionAtAngle(center: CGPoint, angle: CGFloat, radius: CGFloat) -> CGPoint
{
var dx = radius * sin(angle)
var dy = radius * cos(angle)
return CGPoint(x: center.x + dx, y: center.y - dy)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment