Search code examples
iphonecocoa-touchiphone-sdk-3.0quartz-graphicsquartz-2d

How to draw a triangle with QUARTZ 2D?


I'm developing an iPhone app with 3.1.3 SDK.

I'm trying to draw a triangle with transparent fill an 1px black stroke with this code:

CGContextMoveToPoint(ctx, position.X, position.Y - (size.height / 2));
CGContextAddLineToPoint(ctx, position.X - (size.width / 2), position.Y + (size.height / 2));
CGContextAddLineToPoint(ctx, position.X + (size.width / 2), position.Y + (size.height / 2));
CGContextFillPath(ctx);
CGContextSetLineWidth(ctx, 1.0);
CGContextStrokePath(ctx);
CGContextSetFillColorWithColor(ctx, [[UIColor clearColor] CGColor]);

But I get a black filled triangle.

What am I doing wrong?


Solution

  • If you want to draw a path with transparent fill - then you don't need to fill it - just stroke it when draw, so it seems that your CGContextFillPath method is redundant - remove it:

    CGContextMoveToPoint(ctx, position.X, position.Y - (size.height / 2));
    CGContextAddLineToPoint(ctx, position.X - (size.width / 2), position.Y + (size.height / 2));
    CGContextAddLineToPoint(ctx, position.X + (size.width / 2), position.Y + (size.height / 2));
    CGContextSetLineWidth(ctx, 1.0);
    CGContextStrokePath(ctx);