【问题标题】:Draw dashed line in iOS在iOS中绘制虚线
【发布时间】:2015-12-17 04:59:06
【问题描述】:

我使用CGContextiOS 中绘制虚线。 我的代码如下。

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGFloat dashes[] = {5,5};
CGContextSetLineDash(context, 0, dashes, 2);
float startx = x_percentileValues[0];
float starty = orgy-numskipPixels_v*(3-minWeight);
for(int i=0; i<[MeasuredInfos.retval count]; i++) {
     float x = numskipPixels_h*hoursBetweenDates*3+orgx;
     float y = orgy-([[MeasuredInfos.retval objectAtIndex: i] getWeight] - minWeight)*numskipPixels_v;
     CGContextMoveToPoint(context, startx, starty);
     CGContextAddLineToPoint(context, x, y);
     CGContextStrokePath(context);
     startx = x;
     starty = y;


}

如果线的坡度很陡,那很好。如果不陡,则虚线有问题,如附图所示。

我签入了这个link,没有太多关于CGContextSetLineDash 的讨论。

【问题讨论】:

  • 您能描述一下您看到的问题吗?每张图片都有两组虚线,我不清楚你指的是哪一组。另外,虚线看起来不对怎么办?
  • @DougRichardson 你不认为这是问题所在吗? “线的斜率大就好了,不陡就是虚线有问题,见附图。”
  • 我没有说我不认为这是一个问题,我只是不明白你在说什么问题。我看到的唯一可能的问题是陡峭的粗虚线在靠近恒星的右侧有一条额外的细线,但你说陡峭的线看起来很好,所以这不是你正在谈论的问题。我看不出扁平粗线有什么问题,这就是你说的问题所在。究竟是什么问题?
  • @DougRichardson 当然,粗线是问题所在。
  • 好的,所以要清楚一点:顶部图像(带有陡峭粗线的图像)是有问题的图像。那个问题是右手边最后的细线?如果是这样,您应该改写您的问题,因为在其中您说“如果线的斜率很陡,那很好”,但问题是如果线的斜率很陡,那就不好了。

标签: ios cgcontext cgcontextref cgcontextdrawpath


【解决方案1】:

我发现了问题。问题是我在两个单独的函数中使用了两个 CGContextRef。一个在

       - (void)drawRect:(CGRect)rect {
        //Get the CGContext from this view
            CGContextRef context = UIGraphicsGetCurrentContext();

            //Set the stroke (pen) color
            CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
            //Set the width of the pen mark
            CGContextSetLineWidth(context, 2.0);
            float startx = x_percentileValues[0];
            float starty = orgy-numskipPixels_v*(3-minWeight);
            for(int i=0; i<[MeasuredInfos.retval count]; i++) {
                 float x = numskipPixels_h*hoursBetweenDates*3+orgx;
                 float y = orgy-([[MeasuredInfos.retval objectAtIndex: i] getWeight] - minWeight)*numskipPixels_v;
                 CGContextMoveToPoint(context, startx, starty);
                 CGContextAddLineToPoint(context, x, y);
                 CGContextStrokePath(context);
                 startx = x;
                 starty = y;

                 [self drawStar_atX:x andatY:y];
            }

        }

drawStar_atX 中的另一个

-(void)drawStar_atX:(float)xCenter andatY:(float)yCenter
{
    int aSize = 5.0;
    CGFloat color[4] = { 1.0, 0.0, 0.0, 1.0 }; // Red
    CGColorRef aColor = CGColorCreate(CGColorSpaceCreateDeviceRGB(), color);
    CGContextRef context1 = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context1, aSize);

    float  w = 15.0;
    double r = w / 2.0;
    float flip = -1.0;

    CGContextSetFillColorWithColor(context1, aColor);


    CGContextSetStrokeColorWithColor(context1, aColor);

    double theta = 2.0 * M_PI * (2.0 / 5.0); // 144 degrees

    CGContextMoveToPoint(context1, xCenter, r*flip+yCenter);

    for (NSUInteger k=1; k<5; k++)
    {
        float x = r * sin(k * theta);
        float y = r * cos(k * theta);
        CGContextAddLineToPoint(context1, x+xCenter, y*flip+yCenter);
    }

    CGContextClosePath(context1);
    CGContextFillPath(context1);
    return;
}

我从drawRect 打电话给drawStar_atX。两个函数中的 context 和 context1 有一些问题,从第二段开始,虚线的宽度变大了。实际上它们是在不同宽度的不同函数中声明的,应该没有任何关系。现在我解决了这个问题,因为 context 和 context1 具有相同的宽度。仍在学习为什么他们有一些关系。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    • 2017-12-21
    • 2017-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多