Draw lines and fills with a chalk texture in iPhone app

你。 提交于 2020-01-06 07:07:09

问题


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:

Drawing the circle around the pie chart:

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?

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!

Robert


回答1:


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

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.



来源:https://stackoverflow.com/questions/4172107/draw-lines-and-fills-with-a-chalk-texture-in-iphone-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!