Skip to content

Instantly share code, notes, and snippets.

@iRiziya
Created March 10, 2016 10:04
Show Gist options
  • Save iRiziya/9c092499fbae61211b4e to your computer and use it in GitHub Desktop.
Save iRiziya/9c092499fbae61211b4e to your computer and use it in GitHub Desktop.
Circle chart(with 2 circles, inner and outer) using SwiftyCircle lib
import UIKit
class CircleChartVC: UIViewController {
let Points:String = "652"
override func viewDidLoad() {
super.viewDidLoad()
let outerCirc = SwiftyCircle(frame: CGRect(x: 100, y: 20, width: 250, height: 250))
outerCirc.centerFillColor = UIColor(red:0.13, green:0.44, blue:0.63, alpha:1)
outerCirc.strokeColor = UIColor.orangeColor()
outerCirc.strokeWidth = 20
outerCirc.backgroundColor = UIColor.clearColor()
outerCirc.center.x = self.view.center.x
let innerCirc = SwiftyCircle(frame: CGRect(x: 0, y: 0, width: 210, height: 210))
innerCirc.center = outerCirc.center
innerCirc.strokeColor = UIColor.redColor()
innerCirc.strokeWidth = 20
let myString = "\(Points!)\nPOINTS"
print(myString)
var myMutableString = NSMutableAttributedString()
myMutableString = NSMutableAttributedString(
string: myString,
attributes: [NSFontAttributeName:UIFont(
name: "Helvetica Neue",
size: 50.0)!])
//
myMutableString.addAttribute(NSFontAttributeName,
value: UIFont(
name: "Helvetica Neue",
size: 30.0)!,
range: NSRange(
location:Points!.characters.count+1,
length: 6))
innerCirc.text = myMutableString
innerCirc.backgroundColor = UIColor.clearColor()
self.view.addSubview(outerCirc)
self.view.addSubview(innerCirc)
}
}
// Original SwityCircle : https://github.com/BilalReffas/SwiftyCircle
//Customized with a label in center
import UIKit
@IBDesignable public class SwiftyCircle : UIView {
public struct Constants {
let higherCircle = CAShapeLayer()
let lowerCircle = CAShapeLayer()
var label = UILabel() //customized
}
private var constants = Constants()
var text: NSAttributedString? {
didSet {
self.constants.label.attributedText = text
}
}
var textColor : UIColor = UIColor(red:0.13, green:0.44, blue:0.63, alpha:1){
didSet {
self.constants.label.textColor = textColor
}
}
@IBInspectable public var progress : CGFloat = 0.2{
didSet {
setNeedsDisplay()
}
}
@IBInspectable public var roundCap: Bool = true {
didSet {
setNeedsDisplay()
}
}
@IBInspectable public var centerFillColor : UIColor = UIColor.whiteColor(){
didSet {
setNeedsDisplay()
}
}
@IBInspectable public var strokeColor : UIColor = UIColor.whiteColor(){
didSet {
setNeedsDisplay()
}
}
@IBInspectable public var strokeWidth : CGFloat = 7.0 {
didSet {
setNeedsDisplay()
}
}
@IBInspectable public var lowerCirlceStrokeColor : UIColor = UIColor.grayColor(){
didSet {
setNeedsDisplay()
}
}
@IBInspectable public var lowerCirlceStrokeWidth : CGFloat = 3.0 {
didSet {
setNeedsDisplay()
}
}
@IBInspectable public var lowerCirlceProgress : CGFloat = 1.0{
didSet {
setNeedsDisplay()
}
}
@IBInspectable public var drawlowerCirlce : Bool = false{
didSet {
setNeedsDisplay()
}
}
override init(frame: CGRect) {
super.init(frame: frame)
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
public override func drawRect(rect: CGRect) {
super.drawRect(rect)
self.layoutIfNeeded()
self.backgroundColor = UIColor.clearColor()
let frameSize = self.frame.size
let arcCenter : CGPoint = CGPoint(x: frameSize.width / 2.0, y: frameSize.height / 2.0)
let radius : CGFloat = (frameSize.width - 10)/2
let startAngel : CGFloat = CGFloat(-M_PI_2)
let endAngel : CGFloat = CGFloat((M_PI * 2.0) - M_PI_2)
self.constants.label = UILabel(frame: CGRectMake(0,0,110,200))
self.constants.label.attributedText = self.text
self.constants.label.textColor = self.textColor
self.constants.label.textAlignment = .Center
self.constants.label.center = arcCenter
self.constants.label.numberOfLines = 0
self.constants.label.lineBreakMode = .ByWordWrapping
// self.constants.label.font = UIFont.systemFontOfSize(30)
/**Draw the HigherCircle**/
self.constants.higherCircle.path = UIBezierPath(arcCenter: arcCenter, radius: radius, startAngle: startAngel, endAngle: endAngel , clockwise: true).CGPath
self.constants.higherCircle.lineWidth = self.strokeWidth
self.constants.higherCircle.strokeStart = 0.0
self.constants.higherCircle.strokeEnd = self.progress
self.constants.higherCircle.speed = 0.1
self.constants.higherCircle.lineCap = self.roundCap == true ? kCALineCapRound : kCALineCapButt
self.constants.higherCircle.fillColor = self.centerFillColor.CGColor
self.constants.higherCircle.strokeColor = self.strokeColor.CGColor
self.animateCircle(0.2, sender : true)
if self.drawlowerCirlce == true {
/**Draw the LowerCirlcle**/
self.constants.lowerCircle.path = UIBezierPath(arcCenter: arcCenter, radius: radius, startAngle: startAngel, endAngle: endAngel , clockwise: true).CGPath
self.constants.lowerCircle.lineWidth = self.lowerCirlceStrokeWidth
self.constants.lowerCircle.strokeStart = 0.0
self.constants.lowerCircle.strokeEnd = self.lowerCirlceProgress
self.constants.lowerCircle.speed = 0.1
self.constants.lowerCircle.lineCap = self.roundCap == true ? kCALineCapRound : kCALineCapButt
self.constants.lowerCircle.fillColor = UIColor.clearColor().CGColor
self.constants.lowerCircle.strokeColor = self.lowerCirlceStrokeColor.CGColor
self.layer.addSublayer(self.constants.lowerCircle)
}
self.layer.addSublayer(self.constants.higherCircle)
self.addSubview(self.constants.label)
}
func animateCircle(duration: NSTimeInterval, sender : Bool) {
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.duration = duration
animation.fromValue = 0
animation.toValue = sender == true ? self.progress : self.lowerCirlceProgress
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
self.constants.higherCircle.strokeEnd = self.progress
self.constants.higherCircle.addAnimation(animation, forKey: "animateHigherCircle")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment