Swift:
override func drawRect(rect: CGRect) {
let con = UIGraphicsGetCurrentContext()
let radius: CGFloat = CGRectGetWidth(bounds) / 2
//参数2,3是圆心坐标,4是半径,5是开始弧度,6是结束弧度,7是顺时针/逆时针方向
CGContextAddArc(con, radius, radius, radius, 0, CGFloat(2 * M_PI), 0)
CGContextAddArc(con, radius, radius, radius - 50 - CGFloat(2 * 4), CGFloat(2 * M_PI), 0, 1)
//制定填充颜色
CGContextSetFillColorWithColor(con, UIColor(red: 0, green: 52 / 255.0, blue: 118 / 255, alpha: 1).CGColor)
//为制定的形状填充颜色
CGContextFillPath(con)
}
OC:
- (void)drawRect:(CGRect)rect {
CGContextRef con = UIGraphicsGetCurrentContext();
CGFloat radius = self.bounds.size.width/2;
CGContextAddArc(con, radius, radius, radius, 0, 2*M_PI, YES);
CGContextAddArc(con, radius, radius, radius - 50 -2 * 4, 2*M_PI, 0, NO);
CGContextSetFillColorWithColor(con, [UIColor colorWithRed:0 green:52/255.0 blue:118/255.0 alpha:1.0].CGColor);
CGContextFillPath(con);
}