开发者

Draw lines and fills with a chalk texture in iPhone app

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 f开发者_JS百科ollow.

Any help will be gratefully received!

Robert


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜