CGContextRef画不完整的圆

本文介绍了一种使用Objective-C编程语言在iOS平台下绘制不完整圆形进度条的方法。通过UIGraphicsBeginImageContext和CGContextRef等API实现了一个可以根据百分比绘制不同进度的圆形进度条。文章提供了具体的代码实现细节。

就是要这种效果:

代码:

- (void)drawCircleWithPercent:(float)per {
    //开始绘画
    UIGraphicsBeginImageContext(CGSizeMake(70, 70));
    //Quartz 2D 画布
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context,6);//改变线条粗细
    //底部完整圆
    CGContextSetRGBStrokeColor(context, 245/255.0, 215/255.0, 136/255.0, 1);//改变颜色
    CGContextAddArc(context, 35, 35, 32, 0, 2*M_PI, 0);//画圆
    CGContextStrokePath(context);//绘画,这句才是真正的将东西画上了
    //覆盖层,不完整圆
    CGContextSetRGBStrokeColor(context, 127/255.0, 204/255.0, 193/255.0, 1);
    CGContextAddArc(context, 35, 35, 32, 0, 2*M_PI*per, 1);
    CGContextStrokePath(context);
    //结束绘画
    UIImage *destImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    //创建UIImageView并显示在界面上
    UIImageView *imgView = [[UIImageView alloc] initWithImage:destImg];
    imgView.x = 67;
    imgView.y = 123;
    [self.view addSubview:imgView];
}


### 如何使用 `drawEllipse` 函数绘制半 在图形绘制中,`drawEllipse` 函数通常用于绘制完整的椭。然而,如果需要绘制半,则需要结合其他方法来实现部分椭的显示效果。以下是几种可能的实现方式: #### 方法一:通过裁剪矩形限制绘制区域 可以利用 `drawEllipse` 的矩形参数特性,通过调整矩形的大小和位置,仅绘制椭的一部分。例如,在 PyQt5 中,可以通过 `QPainterPath` 来实现更精确的控制。 ```python from PyQt5.QtWidgets import QLabel, QApplication from PyQt5.QtGui import QPainter, QPaintEvent, QPen, QBrush from PyQt5.QtCore import Qt, QRectF class SemiCircleLabel(QLabel): def __init__(self, parent=None): super().__init__(parent=parent) self.setFixedSize(200, 100) # 设置固定大小 def paintEvent(self, event: QPaintEvent): painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) # 抗锯齿 pen = QPen(Qt.black, 2) brush = QBrush(Qt.red) painter.setPen(pen) painter.setBrush(brush) # 使用 drawEllipse 绘制半 rect = QRectF(10, 10, 180, 90) # 矩形范围控制椭形状 painter.drawArc(rect, 90 * 16, 180 * 16) # 绘制半弧度 (起始角度, 扫描角度) app = QApplication([]) label = SemiCircleLabel() label.show() app.exec_() ``` 上述代码中,`drawArc` 是一种替代方案,它允许指定起始角度和扫描角度,从而实现半的绘制[^3]。 --- #### 方法二:使用贝塞尔曲线模拟半 如果目标环境支持直接的半绘制功能,可以借助贝塞尔曲线(Bezier Curve)手动构造半。以下是一个简单的示例: ```python from PyQt5.QtWidgets import QLabel, QApplication from PyQt5.QtGui import QPainter, QPainterPath, QPen, QBrush from PyQt5.QtCore import Qt class BezierSemiCircleLabel(QLabel): def __init__(self, parent=None): super().__init__(parent=parent) self.setFixedSize(200, 100) def paintEvent(self, event): painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) pen = QPen(Qt.black, 2) brush = QBrush(Qt.blue) painter.setPen(pen) painter.setBrush(brush) path = QPainterPath() radius = 90 center_x, center_y = 100, 50 path.moveTo(center_x - radius, center_y) # 起点 path.arcTo(center_x - radius, center_y - radius, 2 * radius, 2 * radius, 180, 180) # 绘制半 painter.drawPath(path) app = QApplication([]) label = BezierSemiCircleLabel() label.show() app.exec_() ``` 这里使用了 `QPainterPath` 和 `arcTo` 方法来创建一个半路径,并通过 `drawPath` 方法进行绘制[^1]。 --- #### 方法三:iOS 平台上的实现 在 iOS 开发中,可以使用 `CGContext` 提供的功能来绘制半。以下是一个基于引用[2]的扩展示例: ```objc +(void)drawSemiCircle:(CGPoint)point withColor:(UIColor*)color withRadius:(float)radius { CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(ctx, [color CGColor]); // 创建半路径 UIBezierPath *semiCirclePath = [UIBezierPath bezierPath]; [semiCirclePath moveToPoint:CGPointMake(point.x - radius, point.y)]; [semiCirclePath addArcWithCenter:CGPointMake(point.x, point.y) radius:radius startAngle:M_PI endAngle:0.0 clockwise:YES]; [semiCirclePath closePath]; // 填充颜色 [[UIColor redColor] setFill]; [semiCirclePath fill]; } ``` 该方法通过 `UIBezierPath` 定义了一个从左到右的半路径,并使用 `addArcWithCenter` 方法指定弧线的角度范围[^2]。 --- ### 注意事项 - 在同框架中,`drawEllipse` 或类似函数的行为可能略有差异,请根据具体环境调整参数。 - 如果需要动态效果,可以结合定时器或动框架逐步调整半径或角度,以实现流畅的过渡效果[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值