关键代码步骤:
- 获取视图的图形上下文。
- 设置填充颜色为红色。
- 计算圆心位置。
- 使用
CGContextAddArc
方法添加圆形路径。
- 使用
CGContextFillPath
方法填充路径。
实现代码:
#import "ViewController.h"
@implementation ViewController
- (void)drawRect:(CGRect)rect {
// 获取当前图形上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 设置填充颜色为红色
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
// 计算圆心位置
CGFloat centerX = CGRectGetMidX(rect);
CGFloat centerY = CGRectGetMidY(rect);
CGFloat radius = 50;
// 添加圆形路径
CGContextAddArc(context, centerX, centerY, radius, 0, 2 * M_PI, 0);
// 填充路径
CGContextFillPath(context);
}
@end