【问题标题】:drawRect Circle inside of circle used to show progressdrawRect Circle inside 用于显示进度
【发布时间】:2013-10-14 06:32:46
【问题描述】:

我试图通过在一个圆圈内填满另一个圆圈来显示进度。有人可以帮我实现这一目标吗?我可能会以错误的方式解决这个问题。 这是我的代码:

- (void)drawRect:(CGRect)rect
{
    rect = CGRectMake(0, 400, 500, 500);
    [[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 500, 500) cornerRadius:50.0] addClip];


    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(contextRef, 2.0);
    CGContextSetRGBFillColor(contextRef, 111/255.0f, 116/255.0f, 128/255.0f, 1.0);
    CGRect circlePoint = (CGRectMake(0, 0, rect.size.width, rect.size.height));
    CGContextFillEllipseInRect(contextRef, circlePoint);

    CGContextBeginPath(contextRef);

    float c = 20; //Number that should cause the green progress to increase and decrease
    CGContextAddArc(contextRef, 250, 250, 250, 0, M_PI, 0);
    CGContextClosePath(contextRef); // could be omitted
    UIColor *color = [UIColor greenColor];
    CGContextSetFillColorWithColor(contextRef, color.CGColor);
    CGContextFillPath(contextRef);
}

这里是它现在的样子的截图。我基本上希望能够使用我的float c; 变量来控制内部绿色的进度。

我尝试过使用CGContextAddArc,但是当我减小“c”的值时,我无法随着“c”的增加而增长到圆圈的大小

【问题讨论】:

    标签: iphone ios geometry progress drawrect


    【解决方案1】:

    用你的 c 表示从哪里到哪里

    // draw the pie part
    CGContextMoveToPoint(_viewContext, centerX, centerY);
    CGContextAddArc(_viewContext, centerX, centerY, pieRadius, radians(startDegrees), radians(endDegrees), 0);
    CGContextClosePath(_viewContext);
    CGContextFillPath(_viewContext);
    
    • startDegrees = 0
    • endDegress = c
    • _viewContext = contextRef
    • centerX/CenterY = 边界中心
    • PieRadius = 你想要的半径:D 像素 IIRC

    将度数转换为弧度

    #define pi 3.14159265
    double radians(double percentage)
    {
        return (percentage/100)*360 * (pi/180);
    }
    

    【讨论】:

    • 它正在工作!你知道为什么进展不只是直线上升吗?它从侧面随机上升。谢谢您的帮助!以下是部分截图: 25%:d.pr/i/CCv6 50%:d.pr/i/caOU 75%:d.pr/i/GbhF
    • 啊,我知道你想要什么了。你想让它从下到上填满圆圈……有点像“玻璃”——这不是随机的。它从 RAD=0 开始。如果您想从底部填充它,只需将其全部绘制为矩形。并将其裁剪成圆形
    猜你喜欢
    • 2020-09-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2014-06-14
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 2012-03-21
    相关资源
    最近更新 更多