如何使用QUARTZ 2D绘制三角形



我正在用3.1.3 SDK开发一款iPhone应用程序。

我正试图用这个代码画一个透明的三角形,用1px的黑色笔画填充:

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

但我得到了一个黑色填充的三角形。

我做错了什么?

如果你想用透明填充来绘制路径-那么你不需要填充它-只需在绘制时笔划它,所以你的CGContextFillPath方法似乎是多余的-删除它:

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

最新更新