The general idea here is to draw an arc using a large line width. The strokeEnd
property of the CAShapeLayer determines what percentage of the arc is drawn. Initially, you want srokeEnd
to be zero, so that nothing is drawn. You can then animate strokeEnd
from 0.0 to 0.43 to display 43% of the arc. This assumes that the bezier path is setup to draw an arc through an angle about 2.3 times more than the angle shown in your image.
In the code below, the center of the arc is at the center of the parent view, and the radius of the arc is chosen so that the outside edge of the arc reaches the edge of the parent view (looks like you'll want to adjust the radius a little). The endAngle
of the arc was chosen so that 43% of the arc looks sort of like the image you posted (you'll want to adjust that as well).
- (void)addPieShapeToView:(UIView *) view
{
int thickness = 40;
int x = view.bounds.size.width / 2;
int y = view.bounds.size.height / 2;
int r = (x < y ? x : y) - thickness / 2;
UIBezierPath *path = [UIBezierPath new];
[path moveToPoint:CGPointMake(x, y - r)];
[path addArcWithCenter:CGPointMake(x, y) radius:r startAngle:-M_PI_2 endAngle:2.88 clockwise:YES];
UIColor *blue = [UIColor blueColor];
UIColor *clear = [UIColor clearColor];
self.timerLayer = [CAShapeLayer new];
self.timerLayer.frame = view.bounds;
self.timerLayer.path = [path CGPath];
self.timerLayer.strokeColor = [blue CGColor];
self.timerLayer.fillColor = [clear CGColor];
self.timerLayer.lineWidth = thickness;
self.timerLayer.strokeEnd = 0;
[view.layer addSublayer:self.timerLayer];
}
- (void)animationForPieShape
{
CABasicAnimation *timerAnimation;
timerAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
timerAnimation.duration = 1;
timerAnimation.fromValue = @0.00;
timerAnimation.toValue = @0.43;
self.timerLayer.strokeEnd = 0.43;
[self.timerLayer addAnimation:timerAnimation forKey:nil];
}