CGContextAddArc和UIBezierPath画弧参数设置

画弧线方法

 CGContextAddArc(CGContextRef c, CGFloat x, CGFloat y, CGFloat radius,CGFloat startAngle, CGFloat endAngle, int clockwise)

理解:方法中的startAngle,endAngle,clockwise是相对于数学坐标(x轴向右,y轴向上)来说的,但是由于ios中的坐标是x向右,y向下,因此最后得到的效果会是相对于x轴的镜像。也就是绘制时,是先按照x向右,y向上的坐标,以指定的方向进行绘制,然后获得的弧线和方向以x为轴,做镜像翻转,这样才获得最终的图形效果。

实际应用:
根据效果设置参数:
1、使用数学坐标(x向右,y向上)时,先获取需要的效果的起点、终点位置及方向,然后将起点、终点及方向都取反,则获得的参数即为填入方法中的参数。
2、使用ios坐标(x向右,y向下)时,先取得需要的效果的起点、终点位置及方向,然后只将方向取反,则起点、终点、取反后的方向即为填入方法中的参数。

根据参数查看效果:
1、使用数学坐标(x向右,y向上)时,将起点、终点及方向都取反
2、使用ios坐标(x向右,y向下)时,只将方向取反

UIBezierPath画弧

- (void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise

通过UIBezierPath画弧时,方法中的startAngle,endAngle,clockwise是相对于ios坐标(x轴向右,y轴向下)来说的,也就是绘制时,直接按照x向右,y向下的坐标,以指定的方向进行绘制,不用用做翻转,直接得到最终的图形效果。

例:

    CGPoint centerPoint = CGPointMake(CGRectGetWidth(rect)/2, CGRectGetHeight(rect)/2);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor yellowColor].CGColor);
    //CGContextRef画弧
    //CGContextMoveToPoint(context, centerPoint.x, centerPoint.y);
    //CGContextAddArc(context, centerPoint.x, centerPoint.y, 100, 0, -M_PI_2, 1);
    
    //UIBezierPath画弧
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint:centerPoint];
    [bezierPath addArcWithCenter:centerPoint radius:100 startAngle:0 endAngle:-M_PI_2 clockwise:0];
    CGContextAddPath(context, bezierPath.CGPath);
    
    CGContextClosePath(context);
    CGContextFillPath(context);
屏幕快照 2019-03-20 下午2.00.55.png

你可能感兴趣的:(CGContextAddArc和UIBezierPath画弧参数设置)