overridefuncdrawRect(rect:CGRect){letπ=M_PIletcontext=UIGraphicsGetCurrentContext()CGContextSetLineWidth(context,20.0)// Back circleCGContextSetStrokeColorWithColor(context,UIColor.blueColor().colorWithAlphaComponent(0.2).CGColor)CGContextAddArc(context,150,150,100,CGFloat(0),CGFloat(2*π),0)CGContextStrokePath(context)letradius=100.0// Create the circle layervarcircle=CAShapeLayer()// Set the center of the circle to be the center of the viewletcenter=CGPointMake(150,150)letfractionOfCircle=3.0/4.0lettwoPi=2.0*Double(M_PI)// The starting angle is given by the fraction of the circle that the point is at, divided by 2 * Pi and less// We subtract M_PI_2 to rotate the circle 90 degrees to make it more intuitive (i.e. like a clock face with zero at the top, 1/4 at RHS, 1/2 at bottom, etc.)letstartAngle=Double(fractionOfCircle)/Double(twoPi)-Double(M_PI_2)letendAngle=0.0-Double(M_PI_2)letclockwise:Bool=true// `clockwise` tells the circle whether to animate in a clockwise or anti clockwise directioncircle.path=UIBezierPath(arcCenter:center,radius:CGFloat(radius),startAngle:CGFloat(startAngle),endAngle:CGFloat(endAngle),clockwise:clockwise).CGPath// Configure the circlecircle.fillColor=UIColor.clearColor().CGColorcircle.strokeColor=UIColor.blueColor().CGColorcircle.lineWidth=20.0circle.lineCap=kCALineCapRound// When it gets to the end of its animation, leave it at 0% stroke filledcircle.strokeEnd=CGFloat(fractionOfCircle)// Add the circle to the parent layerself.layer.addSublayer(circle)// Configure the animationvardrawAnimation=CABasicAnimation(keyPath:"strokeEnd")drawAnimation.repeatCount=1.0// Animate from the full stroke being drawn to none of the stroke being drawndrawAnimation.fromValue=NSNumber(float:0.0)drawAnimation.toValue=NSNumber(double:fractionOfCircle)drawAnimation.duration=0.8drawAnimation.timingFunction=CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseInEaseOut)// Add the animation to the circlecircle.addAnimation(drawAnimation,forKey:"drawCircleAnimation")}