在iPhone应用程序中绘制线条并用粉笔纹理填充 [英] Draw lines and fills with a chalk texture in iPhone app

查看:86
本文介绍了在iPhone应用程序中绘制线条并用粉笔纹理填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have code in my iPhone app to draw a pie chart in a UIView. All is great but I need to be able to use a chalk-like texture to draw the outlines and then to fill in the different colours of the pie chart.

这里是从每个区域的现有代码中提取:

Here are extracts from the existing code for each area:

在饼图周围绘制圆圈:

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);

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

... and drawing a segment of the pie:

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);

...然后用颜色填充:

... then filling it with a colour fill:

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);

忽略以下事实:我并未针对所使用的许多变量粘贴在变量声明中,希望代码是不言自明的。

Ignoring the fact that I haven't pasted in the variable declarations for many of the variables used, I hope the code is fairly self-explanatory.

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

Now: How could I change the code to use a texture for the actual drawing of the lines/arcs?

类似地,我如何使用纹理填充而不是颜色填充?

Similarly, how can I use a texture fill instead of a colour fill?

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

I'm trying to get this app finished off and this is my last stumbling block. Other questions on SO and elsewhere on the web just don't seem to answer the question in a way I can follow.

任何帮助都会感激不尽!

Any help will be gratefully received!

罗伯特

推荐答案

知道这已经过时了,但是可以设置填充或描边样式使用以下简单代码段:

Know this is outdated but one can set fill or stroke patterns using this simple code snippet:

  // 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文档中进行深入挖掘。

Hope it will help someone. There are also other great advanced possibilities - you can manipulate patterns in many ways: shift, scale, rotate, use colored or stencil patters and many many others, but this requires deep digging in api docs.

这篇关于在iPhone应用程序中绘制线条并用粉笔纹理填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆