【问题标题】:Draw hollow circle divided into equal parts画空心圆等分
【发布时间】:2013-05-28 09:48:04
【问题描述】:

我想画一个空心圆,如下图所示

我得到下面的代码 sn-p 来绘制空心圆

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor whiteColor];

        // Determine our start and stop angles for the arc (in radians)
        startAngle = M_PI * 1.5;
        endAngle = startAngle + (M_PI * 2);

    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    // Display our percentage as a string
    NSString* textContent = [NSString stringWithFormat:@"%d", self.percent];

    UIBezierPath* bezierPath = [UIBezierPath bezierPath];

    // Create our arc, with the correct angles
    [bezierPath addArcWithCenter:CGPointMake(rect.size.width / 2, rect.size.height / 2)
                          radius:130
                      startAngle:startAngle
                        endAngle:(endAngle - startAngle) * (_percent / 100.0) + startAngle
                       clockwise:YES];


    // Set the display for the path, and stroke it
    bezierPath.lineWidth = 50;
    [[UIColor redColor] setStroke];
    [bezierPath stroke];

    // Text Drawing
    CGRect textRect = CGRectMake((rect.size.width / 2.0) - 71/2.0, (rect.size.height / 2.0) - 45/2.0, 71, 45);
    [[UIColor blackColor] setFill];
    [textContent drawInRect: textRect withFont: [UIFont fontWithName: @"Helvetica-Bold" size: 42.5] lineBreakMode: NSLineBreakByWordWrapping alignment: NSTextAlignmentCenter];
}

我也需要空心圆圈中的分隔线,如上图所示。任何建议都会有所帮助....

【问题讨论】:

标签: iphone ios uibezierpath


【解决方案1】:

看看HKCircularProgressView。 我将它用于我自己的项目,根据您的需要扩展功能应该很容易。

【讨论】:

  • 我试过代码,它最适合我的需要。我发现代码有一个问题,当我将当前值设为 www1.datafilehost.com/d/87da4f50 ...您是否遇到同样的问题?
  • 嗯,很遗憾,我无法在我的工作区查看屏幕截图,因为防火墙阻止了托管站点。实际上我没有遇到 HKCircularProgressView 的任何重大问题。如果您打算使用该代码,我建议您在 HKCircularProgressView 的 github 存储库上报告问题。
【解决方案2】:

没有准确回答所问的问题,而是一个简单的解决方案,将任何圆圈分成三个相等的部分,解决了我的目的-

- (void)drawRect:(CGRect)rect {

CGPoint center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f, CGRectGetHeight(self.bounds) / 2.f);
CGFloat radius = 8.f;

UIBezierPath *portionPath1 = [UIBezierPath bezierPath];
[portionPath1 moveToPoint:center];
[portionPath1 addArcWithCenter:center radius:radius startAngle:radians(0) endAngle:radians(120) clockwise:YES];
[portionPath1 closePath];

[[UIColor greenColor] setFill];
[portionPath1 fill];

UIBezierPath *portionPath2 = [UIBezierPath bezierPath];
[portionPath2 moveToPoint:center];
[portionPath2 addArcWithCenter:center radius:radius startAngle:radians(120) endAngle:radians(240) clockwise:YES];
[portionPath2 closePath];

[[UIColor yellowColor] setFill];
[portionPath2 fill];

UIBezierPath *portionPath3 = [UIBezierPath bezierPath];
[portionPath3 moveToPoint:center];
[portionPath3 addArcWithCenter:center radius:radius startAngle:radians(240) endAngle:radians(360) clockwise:YES];
[portionPath3 closePath];

[[UIColor blueColor] setFill];
[portionPath3 fill]; }

将这两行放在最上面。

#define PI 3.14159265358979323846
static inline float radians(double degrees) { return degrees * PI / 180; }

希望这对其他人也有帮助。 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 2020-05-22
    • 1970-01-01
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多