使用iOS中的Core Graphics绘制VU表

Raj*_*jal 2 core-graphics objective-c ios

我正在尝试使用Core Graphics绘制一个类似的图像:

在此输入图像描述

我能够绘制主弧,但我无法理解,如何将弧分成零件/在弧上绘制刻度?我当前绘制弧的代码是:

[path addArcWithCenter:point radius:radius startAngle:DEGREES_TO_RADIANS(specific_start_angle) endAngle:DEGREES_TO_RADIANS(specific_end_angle) clockwise:NO];
Run Code Online (Sandbox Code Playgroud)

我尝试使用,strokeWithBlendMode但我面临着毕业或蜱的位置问题.

Ham*_*ish 5

Teja的解决方案可以很好地为您服务,但它确实需要您为毕业计算自己的起点和终点.

我建议你创建一个函数,让你在弧的给定角度绘制刻度,在给定长度的情况下计算刻度的起点和终点.

static inline void drawGraduation(CGPoint center, CGFloat radius, CGFloat angle, CGFloat length, CGFloat width, CGColorRef color) {

    CGContextRef c = UIGraphicsGetCurrentContext();

    CGFloat radius2 = radius+length; // The radius of the end points of the graduations
    CGPoint p1 = (CGPoint){cos(angle)*radius+center.x, sin(angle)*radius+center.y}; // the start point of the graduation
    CGPoint p2 = (CGPoint){cos(angle)*radius2+center.x, sin(angle)*radius2+center.y}; // the end point of the graduation

    CGContextMoveToPoint(c, p1.x, p1.y);
    CGContextAddLineToPoint(c, p2.x, p2.y);

    CGContextSetStrokeColorWithColor(c, color);
    CGContextSetLineWidth(c, width);

    CGContextStrokePath(c);
}
Run Code Online (Sandbox Code Playgroud)

然后,当您为VU表的主刻度绘制弧时,可以在for循环(或者您想要这样做)中调用它.您还可以按给定的时间间隔轻松自定义给定刻度的颜色,宽度和长度(例如,此代码每5个刻度会产生一条更粗更长的红线).

- (void)drawRect:(CGRect)rect {

    CGRect r = self.bounds;

    CGFloat startAngle = -M_PI*0.2; // start angle of the main arc
    CGFloat endAngle = -M_PI*0.8; // end angle of the main arc

    NSUInteger numberOfGraduations = 16;
    CGPoint center = (CGPoint){r.size.width*0.5, r.size.height*0.5}; // center of arc
    CGFloat radius = (r.size.width*0.5)-20; // radius of arc

    CGFloat maxGraduationWidth = 1.5; // the maximum graduation width
    CGFloat maxGraduationWidthAngle = maxGraduationWidth/radius; // the maximum graduation width angle (used to prevent the graduations from being stroked outside of the main arc)

    // draw graduations
    CGFloat deltaArc = (endAngle-startAngle+maxGraduationWidthAngle)/(numberOfGraduations-1); // the change in angle of the arc
    CGFloat startArc = startAngle-(maxGraduationWidthAngle*0.5); // the starting angle of the arc

    for (int i = 0; i < numberOfGraduations; i++) {
        if (i % 5 == 0) {
            drawGraduation(center, radius, startArc+(i*deltaArc), 14, 1.5, [UIColor redColor].CGColor); // red graduation every 5 graduations.
        } else {
            drawGraduation(center, radius, startArc+(i*deltaArc), 10, 1, [UIColor blackColor].CGColor);
        }

    }

    // draw main arc
    UIBezierPath* mainArc = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:NO];
    [[UIColor blackColor] setStroke];
    mainArc.lineWidth = 2;
    [mainArc stroke];

}
Run Code Online (Sandbox Code Playgroud)

产量

在此输入图像描述


完整项目:https://github.com/hamishknight/VU-Meter-Arc