如何使用coreldraw绘制Arc

时间:2013-05-08 07:30:08

标签: ios objective-c cocoa-touch core-graphics

我想使用核心绘制绘制如下图所示的弧。

enter image description here

不完全相同,但我设置了背景图像。我需要根据文件下载绘制弧。现在我有下载数量的百分比。我怎么能用核心抽奖来做呢?

3 个答案:

答案 0 :(得分:1)

上面的图片只是两个圆圈 - 一个是在另一个圆圈上切洞。这不是你想要完成的。

您需要使用CGContextAddArc

做这样的事情:

  • 打开路径
  • 转到某一点
  • 开始使用CGContextAddArc
  • 绘制Arc
  • 将(并画线)向内移动到圆弧中心所需的切片宽度
  • 画一个向后的圆弧
  • 关闭路径

答案 1 :(得分:0)

使用中点算法,如果你想绘制圆圈,使同心圆提供圆圈半径的差异

答案 2 :(得分:0)

我有你想要的东西:

// CircularProgress.h

#import <UIKit/UIKit.h>

@interface CircularProgress : UIView

@property (nonatomic, assign) CGFloat percent;

@end

// CircularProgress.m

#import "CircularProgress.h"

@implementation CircularProgress

- (void)setPercent:(CGFloat)percent
{
    _percent = percent;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGRect bounds = [self bounds];
    CGPoint center = CGPointMake(bounds.size.width / 2.0, bounds.size.height / 2.0);

    CGFloat lineWidth = 8.0;
    CGFloat innerRadius = (bounds.size.width / 2.0) - lineWidth;
    CGFloat outerRadius = innerRadius + lineWidth;
    CGFloat startAngle = -((float)M_PI / 2.0);
    CGFloat endAngle = ((self.percent / 100.0) * 2 * (float)M_PI) + startAngle;

    UIBezierPath *processBackgroundPath = [UIBezierPath bezierPath];
    processBackgroundPath.lineWidth = lineWidth;
    CGFloat radius = (self.bounds.size.width - lineWidth) / 2.0;
    CGFloat fullAngle = (2.0 * (float)M_PI) + startAngle;
    [processBackgroundPath addArcWithCenter:center radius:radius startAngle:startAngle endAngle:fullAngle clockwise:YES];
    [[UIColor whiteColor] set];
    [processBackgroundPath stroke];

    CGMutablePathRef progressPath = CGPathCreateMutable();
    CGPathMoveToPoint(progressPath, NULL, center.x, center.y - innerRadius);
    CGPathAddArc(progressPath, NULL, center.x, center.y, innerRadius, startAngle, endAngle, YES);
    CGPathAddArc(progressPath, NULL, center.x, center.y, outerRadius, endAngle, startAngle, NO);
    CGPathCloseSubpath(progressPath);

    UIColor *aColor = [UIColor colorWithRed:0.941 green:0.776 blue:0.216 alpha:1.0];
    [aColor setFill];
    CGContextAddPath(context, progressPath);
    CGContextFillPath(context);

    CGPathRelease(progressPath);
}

@end

您只需要创建一个所需大小的CircularProgress类对象(它应该是正方形),并将其作为子视图添加到主视图中。然后只需更改percent值即可享受结果。现在颜色和宽度都是硬编码的,因为它不是完成的控件,但你应该抓住这个想法。

相关问题