0

我的 iPhone 应用程序中有代码可以在 UIView 中绘制饼图。一切都很好,但我需要能够使用类似粉笔的纹理来绘制轮廓,然后填充饼图的不同颜色。

以下是每个区域现有代码的摘录:

在饼图周围画一个圆圈:

CGContextRef ctx = UIGraphicsGetCurrentContext();

CGContextSetRGBStrokeColor(ctx, 1.0, 1.0, 1.0, 1.0);
CGContextSetLineWidth(ctx, 8.0);

// Draw a thin line around the circle
CGContextAddArc(ctx, x, y, r, 0.0, 360.0*M_PI/180.0, 0);
CGContextClosePath(ctx);
CGContextDrawPath(ctx, kCGPathStroke);

...并绘制饼图的一部分:

CGContextSetRGBStrokeColor(ctx, 1.0, 1.0, 1.0, 1.0);
            CGContextSetLineWidth(ctx, 8.0);
            CGContextMoveToPoint(ctx, x, y);
            CGContextAddArc(ctx, x, y, r, (startDeg-90)*M_PI/180.0, (endDeg-90)*M_PI/180.0, 0);
            CGContextClosePath(ctx);
            CGContextDrawPath(ctx, kCGPathStroke);

...然后用颜色填充填充它:

CGContextSetRGBFillColor(ctx, color.red, color.green, color.blue, color.alpha );
            CGContextMoveToPoint(ctx, x, y);
            CGContextAddArc(ctx, x, y, r, (startDeg-90)*M_PI/180.0, (endDeg-90)*M_PI/180.0, 0);
            CGContextClosePath(ctx);
            CGContextFillPath(ctx);

忽略我没有粘贴许多使用的变量的变量声明这一事实,我希望代码是相当不言自明的。

现在:如何更改代码以使用纹理来实际绘制线/弧?

同样,如何使用纹理填充而不是颜色填充?

我正在努力完成这个应用程序,这是我的最后一个绊脚石。SO和网络上其他地方的其他问题似乎并没有以我可以理解的方式回答这个问题。

任何帮助将不胜感激!

罗伯特

4

1 回答 1

4

知道这是过时的,但可以使用这个简单的代码片段设置填充或描边模式:

  // Somewhere in initialization code.
  UIColor *pattern = [UIColor colorWithPatternImage:[UIImage imageNamed:@"pattern.png"]];
  CGColorRef fillColor = [pattern CGColor];
  CGColorRef strokeColor = [pattern CGColor];

  // Probably in |draw| method call.
  CGContextSetStrokeColorWithColor(context, strokeColor);
  CGContextSetFillColorWithColor(context, fillColor);

希望它会帮助某人。还有其他很棒的高级可能性 - 您可以通过多种方式操纵图案:移位、缩放、旋转、使用彩色或模板图案以及许多其他方式,但这需要深入挖掘 api 文档。

于 2011-04-26T22:52:45.737 回答