How to draw a triangle with QUARTZ 2D?

做~自己de王妃 提交于 2019-12-09 23:41:07

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/6545548/how-to-draw-a-triangle-with-quartz-2d

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