简体   繁体   English

画一个圆的百分比

[英]Draw a percentage of a circle

I would like to draw a circle like those in the picture in objective-c : 我想画一个像Objective-c图片中的圆圈:

海伯格图像
(source: hostingpics.net ) (来源: hostingpics.net

I just want to draw the blue circle filled with the corresponding percentage starting from the bottom. 我只想从底部开始绘制一个蓝色圆圈,上面填充有相应的百分比。 (NB : the range of the percentage is between 0 and 100) Don't worry about the tick in the center, it's just an imageView added over the circle view. (注意:百分比范围在0到100之间)不用担心中间的刻度线,它只是在圆视图上添加的imageView。

You could make 2 image views. 您可以制作2张图片。 One would contain your circle with the check mark, and the rest transparent. 一个将包含您的带有复选标记的圆圈,其余部分将变为透明。 The other image would be below that, with a light blue rectangle. 另一个图像将在其下方,并带有一个浅蓝色矩形。 As the percentage increases, you raise the blue rectangle more and more by modifying it's frame. 随着百分比的增加,您可以通过修改其框架来越来越多地抬起蓝色矩形。 It would be visible of course through the transparent part of the image on top. 当然,通过顶部图像的透明部分可以看到它。

Hope this helps! 希望这可以帮助!

// Draw circle of radius r and center X,Y
CGRect r;
r.origin.y = Y-radius;
r.origin.x = X-radius;
r.size.width = 2*radius;
r.size.height 2*radius;

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(ctx, r);
CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor lightGray] CGColor]));
CGContextFillPath(ctx);

// Draw a blue color filled path. Below is half circle filled with blue color.
CGContextBeginPath(ctx);
CGContextAddArc(ctx, X, Y, r, -M_PI_2, M_PI_2, 1);
CGContextClosePath(ctx); // could be omitted
CGContextSetFillColorWithColor(ctx, [UIColor blueColor].CGColor);
CGContextFillPath(ctx);

Idea is to draw a circle and fill it with light gray color and than draw and close path from arc of the circle (whatever angle is required) and fill it with blue color. 想法是绘制一个圆并用浅灰色填充它,然后从圆弧(无论需要什么角度)绘制并封闭路径,然后用蓝色填充。

Adding to Levi's suggestion. 添加到李维斯的建议。

You can scale the rect image using CGAffineTransformScale . 您可以使用CGAffineTransformScale缩放矩形图像。

eg: 例如:

CGAffineTransform transform = self.rectImageView.transform;
float scale = 0.5;
self.rectImageView.transform = CGAffineTransformScale(transform, 1, scale);

This will scale the image to 50% 这会将图像缩放到50%

to set it back to 100%, just set scale = 2. 将其设置回100%,只需将scale设置为2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM