Skip to content

Instantly share code, notes, and snippets.

@Athosone
Last active January 24, 2016 15:19
Show Gist options
  • Save Athosone/b0bc412b0c154dbcf1e2 to your computer and use it in GitHub Desktop.
Save Athosone/b0bc412b0c154dbcf1e2 to your computer and use it in GitHub Desktop.
//Added animation
import UIKit
extension CALayer
{
func moveToFront()
{
if let superLayerUnwrp = self.superlayer
{
self.removeFromSuperlayer()
superLayerUnwrp.addSublayer(self)
}
}
}
@IBDesignable
class EA_CircleView: UIView {
var shapesLayer = [CAShapeLayer]()
var circleLayer = [CAShapeLayer]()
@IBInspectable var firstColor: UIColor = UIColor(red: (37.0/255.0), green: (252.0/255), blue: (244.0/255.0), alpha: 1.0)
@IBInspectable var secondColor: UIColor = UIColor(red: (171.0/255.0), green: (250.0/255), blue: (81.0/255.0), alpha: 1.0)
@IBInspectable var thirdColor: UIColor = UIColor(red: (238.0/255.0), green: (32.0/255), blue: (53.0/255.0), alpha: 1.0)
@IBInspectable var galopCircle:CGFloat = 0.0
@IBInspectable var trotCircle:CGFloat = 0.0
@IBInspectable var pasCircle:CGFloat = 0.0
@IBInspectable var circleSize:CGFloat = 20.0
@IBInspectable var radiusArc:CGFloat = 200.0
let π:CGFloat = CGFloat(M_PI)
//Draw circle
override func drawRect(rect: CGRect) {
if CGRectGetWidth(rect) < CGRectGetHeight(rect){
self.radiusArc = (CGRectGetWidth(rect) - self.circleSize) / 2.0
}else{
self.radiusArc = (CGRectGetHeight(rect) - self.circleSize) / 2.0
}
self.addCirle(self.radiusArc, color: self.firstColor, value: self.galopCircle + self.trotCircle + self.pasCircle)
self.addCirle(self.radiusArc, color: self.secondColor, value: self.trotCircle + self.pasCircle)
self.addCirle(self.radiusArc, color: self.thirdColor, value: self.pasCircle)
var i = 0
self.shapesLayer.forEach { (shapeLine:CAShapeLayer) -> () in
let animation = CABasicAnimation(keyPath: "strokeEnd")
let circleAnimation:CAKeyframeAnimation = CAKeyframeAnimation(keyPath: "position")
let circle = self.circleLayer[i]
layer.addSublayer(circle)
circle.moveToFront()
animation.duration = EA_DesignHelper.circleViewAnimationDuration
animation.toValue = 1
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
animation.fillMode = kCAFillModeForwards
animation.removedOnCompletion = false
shapeLine.addAnimation(animation, forKey: nil)
circleAnimation.duration = EA_DesignHelper.circleViewAnimationDuration
circleAnimation.path = shapeLine.path;
circleAnimation.calculationMode = kCAAnimationPaced;
circleAnimation.delegate = self;
circleAnimation.removedOnCompletion = false
circleAnimation.fillMode = kCAFillModeForwards
circleAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
circle.addAnimation(circleAnimation, forKey: "position")
i = i + 1
}
}
func addCirle(arcRadius: CGFloat, color: UIColor, value:CGFloat) {
let lineShape = CAShapeLayer()
let X = CGRectGetMidX(self.bounds)
let Y = CGRectGetMidY(self.bounds)
let endAngle = π * 2 * value
let path = UIBezierPath(arcCenter: CGPoint(x: X, y: Y), radius: arcRadius, startAngle: 0, endAngle: endAngle, clockwise: true)
path.lineWidth = self.circleSize
//color.setStroke()
lineShape.path = path.CGPath
lineShape.lineWidth = self.circleSize
lineShape.strokeColor = color.CGColor
lineShape.fillColor = UIColor.clearColor().CGColor
lineShape.strokeEnd = 0
self.shapesLayer.append(lineShape)
layer.addSublayer(lineShape)
//path.stroke()
//let x: CGFloat = (arcRadius * cos(endAngle) + X) - self.circleSize/2
// let y: CGFloat = arcRadius * sin(endAngle) + Y - self.circleSize/2
let pathMiddle = UIBezierPath(ovalInRect: CGRectMake(0 - self.circleSize/2, 0 - self.circleSize/2, self.circleSize, self.circleSize)).CGPath
self.addOval(0.0, path: pathMiddle, strokeStart: 0, strokeEnd: 1.0, strokeColor: color, fillColor: color, shadowRadius: 5.0, shadowOpacity: 0.5, shadowOffsset: CGSizeZero)
}
func addOval(lineWidth: CGFloat, path: CGPathRef, strokeStart: CGFloat, strokeEnd: CGFloat, strokeColor: UIColor, fillColor: UIColor, shadowRadius: CGFloat, shadowOpacity: Float, shadowOffsset: CGSize) {
let arc = CAShapeLayer()
arc.lineWidth = lineWidth
arc.path = path
arc.strokeStart = strokeStart
arc.strokeEnd = strokeEnd
arc.strokeColor = strokeColor.CGColor
arc.fillColor = fillColor.CGColor
arc.shadowColor = UIColor.blackColor().CGColor
arc.shadowRadius = shadowRadius
arc.shadowOpacity = shadowOpacity
arc.shadowOffset = shadowOffsset
// layer.addSublayer(arc)
self.circleLayer.append(arc)
}
//Sum of the values must be equal to 1
func updateViewWithDatas(galop:CGFloat, trot:CGFloat, pas:CGFloat)
{
self.galopCircle = galop
self.trotCircle = trot
self.pasCircle = pas
self.setNeedsDisplay()
}
}
@Athosone
Copy link
Author

Need to refactor this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment