http://i.stack.imgur.com/oh0DL.png

- (void)drawRect:(CGRect)rect {

NSArray *data = @[@30, @15, @5, @17, @3, @10, @20];

// 1. context CGContextRef cxtRef = UIGraphicsGetCurrentContext();

CGPoint center = CGPointMake(150, 150); CGFloat radius = 150; __block CGFloat startAngle = 0; [data enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

// 2. create path
CGFloat endAngle = obj.floatValue / 100 * M_PI * 2 + startAngle;
UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
[circlePath addLineToPoint:center];

// 3. add path
CGContextAddPath(cxtRef, circlePath.CGPath);

// set color
[[UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0) green:((float)arc4random_uniform(256) / 255.0) blue:((float)arc4random_uniform(256) / 255.0) alpha:1.0] setFill];

// 4. render
CGContextDrawPath(cxtRef, kCGPathFill);

// reset angle
startAngle = endAngle;

}];

}
override func draw(_ rect: CGRect) {

// define data to create pie chart let data: [Int] = [30, 15, 5, 17, 3, 10, 20]

// 1. find center of draw rect let center: CGPoint = CGPoint(x: rect.midX, y: rect.midY)

// 2. calculate radius of pie let radius = min(rect.width, rect.height) / 2.0

var startAngle: CGFloat = 0.0 for value in data {

// 3. calculate end angle for slice let endAngle = CGFloat(value) / 100.0 * CGFloat.pi * 2.0 + startAngle

// 4. create UIBezierPath for slide let circlePath = UIBezierPath(arcCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)

// 5. add line to center to close path circlePath.addLine(to: center)

// 6. set fill color for current slice UIColor(red: (CGFloat(arc4random_uniform(256)) / 255.0), green: (CGFloat(arc4random_uniform(256)) / 255.0), blue: (CGFloat(arc4random_uniform(256)) / 255.0), alpha: 1.0).setFill()

// 7. fill slice path circlePath.fill()

// 8. set end angle as start angle for next slice startAngle = endAngle }