最近项目需要,想做一个渐变色的刻度盘,圆形按钮滑动控制,所以 用oc写了一下,代码没附上,想看代码可以私信联系,效果如下图。
部分代码
self.drawCenter = CGPointMake(self.frame.size.width / 2.0, self.frame.size.height / 2.0);
self.radius = self.circleRadius;
self.circleStartPoint = CGPointMake(self.drawCenter.x, self.drawCenter.y - self.circleRadius);
CGContextRef ctx = UIGraphicsGetCurrentContext();
//圆形的背景颜色
CGContextSetStrokeColorWithColor(ctx, self.backgroundTintColor.CGColor);
CGContextSetLineWidth(ctx, self.circleBorderWidth);
CGContextAddArc(ctx, self.drawCenter.x, self.drawCenter.y, self.radius, 0, 2 * M_PI, 0);
CGContextDrawPath(ctx, kCGPathStroke);
//加载的进度
UIBezierPath *loadPath = [UIBezierPath bezierPath];
CGFloat loadStart = -M_PI_2;
CGFloat loadCurre = loadStart + 2 * M_PI * self.loadProgress;
[loadPath addArcWithCenter:self.drawCenter
radius:self.radius
startAngle:loadStart
endAngle:loadCurre
clockwise:YES];
CGContextSaveGState(ctx);
CGContextSetShouldAntialias(ctx, YES);
CGContextSetLineWidth(ctx, self.circleBorderWidth);
CGContextSetStrokeColorWithColor(ctx, self.maximumTrackTintColor.CGColor);
CGContextAddPath(ctx, loadPath.CGPath);
CGContextDrawPath(ctx, kCGPathStroke);
CGContextRestoreGState(ctx);
//起始位置做圆滑处理
CGContextSaveGState(ctx);
CGContextSetShouldAntialias(ctx, YES);
CGContextSetFillColorWithColor(ctx, self.minimumTrackTintColor.CGColor);
CGContextAddArc(ctx, self.circleStartPoint.x, self.circleStartPoint.y, self.circleBorderWidth / 2.0, 0, M_PI * 2, 0);
CGContextDrawPath(ctx, kCGPathFill);
CGContextRestoreGState(ctx);
//value
UIBezierPath *circlePath = [UIBezierPath bezierPath];
CGFloat originstart = -M_PI_2;
CGFloat currentOrigin = originstart + 2 * M_PI * self.value;
[circlePath addArcWithCenter:self.drawCenter
radius:self.radius
startAngle:originstart
endAngle:currentOrigin
clockwise:YES];
CGContextSaveGState(ctx);
CGContextSetShouldAntialias(ctx, YES);
CGContextSetLineWidth(ctx, self.circleBorderWidth);
CGContextSetStrokeColorWithColor(ctx, self.minimumTrackTintColor.CGColor);
CGContextAddPath(ctx, circlePath.CGPath);
CGContextDrawPath(ctx, kCGPathStroke);
CGContextRestoreGState(ctx);
/*
* 计算移动点的位置
* alpha = 移动点相对于起始点顺时针扫过的角度(弧度)
* x = r * sin(alpha) + 圆心的x坐标, sin在0-PI之间为正,PI-2*PI之间为负
* y 可以通过r * cos(alpha) + 圆心的y坐标来计算。
* 不过我这里用了另外一个比较投机的方法,先算出亮点连线在y轴上投影的长度,然后根据移动点在y轴上相对于圆心的位置将这个绝对长度a和圆心y坐标相加减。
*/
double alpha = self.value * 2 * M_PI;
double x = self.radius * sin(alpha) + self.drawCenter.x;
double y = sqrt(self.radius * self.radius - pow((self.drawCenter.x - x), 2)) + self.drawCenter.y;
double a = y - self.drawCenter.y;
if (self.value <= 0.25 || self.value > 0.75) {
y = self.drawCenter.y - a;
}
self.lastPoint = CGPointMake(x, y);
self.thumbView.center = self.lastPoint;