У меня есть то, что вы хотите:
// CircularProgress.h
#import <UIKit/UIKit.h>
@interface CircularProgress : UIView
@property (nonatomic, assign) CGFloat percent;
@end
// CircularProgress.m
#import "CircularProgress.h"
@implementation CircularProgress
- (void)setPercent:(CGFloat)percent
{
_percent = percent;
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect bounds = [self bounds];
CGPoint center = CGPointMake(bounds.size.width/2.0, bounds.size.height/2.0);
CGFloat lineWidth = 8.0;
CGFloat innerRadius = (bounds.size.width/2.0) - lineWidth;
CGFloat outerRadius = innerRadius + lineWidth;
CGFloat startAngle = -((float)M_PI/2.0);
CGFloat endAngle = ((self.percent/100.0) * 2 * (float)M_PI) + startAngle;
UIBezierPath *processBackgroundPath = [UIBezierPath bezierPath];
processBackgroundPath.lineWidth = lineWidth;
CGFloat radius = (self.bounds.size.width - lineWidth)/2.0;
CGFloat fullAngle = (2.0 * (float)M_PI) + startAngle;
[processBackgroundPath addArcWithCenter:center radius:radius startAngle:startAngle endAngle:fullAngle clockwise:YES];
[[UIColor whiteColor] set];
[processBackgroundPath stroke];
CGMutablePathRef progressPath = CGPathCreateMutable();
CGPathMoveToPoint(progressPath, NULL, center.x, center.y - innerRadius);
CGPathAddArc(progressPath, NULL, center.x, center.y, innerRadius, startAngle, endAngle, YES);
CGPathAddArc(progressPath, NULL, center.x, center.y, outerRadius, endAngle, startAngle, NO);
CGPathCloseSubpath(progressPath);
UIColor *aColor = [UIColor colorWithRed:0.941 green:0.776 blue:0.216 alpha:1.0];
[aColor setFill];
CGContextAddPath(context, progressPath);
CGContextFillPath(context);
CGPathRelease(progressPath);
}
@end
Вам просто нужно создать объект CircularProgress
класс желаемого размера (он должен быть квадратным) и добавить его как подпункт к вашему основному виду. Затем просто измените значение percent
и получите результат. Цвет и ширина жестко закодированы, потому что это не законченный контроль, но вы должны поймать идею.
<вставить шутку об использовании дуги для дуги> – Fonix
посмотрите на эту статью http://www.raywenderlich.com/33193/core-graphics-tutorial-arcs-and-paths – tkanzakic