iOS 使用UIBezierPath绘制圆型, 并且根据半径, 角度, 长度在圆外任意一点绘制线段, 通常用于饼状图的文字说明

继承UIView, 自定义一个View

重写方法

- (void)drawRect:(CGRect)rect

完整代码: 

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 100) radius:50 startAngle:0 endAngle:360 clockwise:YES];
    
    [path stroke];
    
    UIBezierPath *path2 = [UIBezierPath bezierPath];
    [path2 moveToPoint:CGPointMake(100, 100)];
    [path2 addLineToPoint:[self calcCircleCoordinateWithCenter:CGPointMake(100, 100) angle:10 radius:60]];
    [path2 stroke];
}

#pragma mark 计算圆圈上点在IOS系统中的坐标
- (CGPoint)calcCircleCoordinateWithCenter:(CGPoint)center angle:(CGFloat)angle radius:(CGFloat)radius
{
    CGFloat x2 = radius * cosf(angle * M_PI / 180);
    CGFloat y2 = radius * sinf(angle * M_PI / 180);
    return CGPointMake(center.x + x2, center.y - y2);
}


你可能感兴趣的:(iOS 使用UIBezierPath绘制圆型, 并且根据半径, 角度, 长度在圆外任意一点绘制线段, 通常用于饼状图的文字说明)