【问题标题】:How to draw a triangle with QUARTZ 2D?如何使用 QUARTZ 2D 绘制三角形?
【发布时间】:2011-07-01 08:37:48
【问题描述】:

我正在使用 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]);

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

我做错了什么?

【问题讨论】:

  • 您应该使用最新版本的 SDK!您仍然可以针对较旧的操作系统。

标签: iphone cocoa-touch iphone-sdk-3.0 quartz-graphics quartz-2d


【解决方案1】:

如果你想绘制一个透明填充的路径——那么你不需要填充它——在绘制的时候直接描边,所以看起来你的 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);

【讨论】:

  • 上例中的ctx是什么
  • 它是对当前图形上下文 (CGContextRef) 的引用,您在 drawRect: 方法中绘制。您通常使用 UIGraphicsGetCurrentContext() 函数获取它
猜你喜欢
  • 1970-01-01
  • 2011-02-19
  • 1970-01-01
  • 1970-01-01
  • 2021-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多