【问题标题】:Trouble drawing CoreGraphics lines in drawRect() method在 drawRect() 方法中绘制 CoreGraphics 线时遇到问题
【发布时间】:2012-02-05 16:05:20
【问题描述】:

我正在学习斯坦福 CS193p 课程并尝试绘制图表。绘制轴有效,但我无法绘制图表本身。我收到消息了

CGContextAddLineToPoint: no current point.

当我尝试画画时。这是代码。

- (void)drawRect:(CGRect)rect
{
NSLog(@"Drawing Graph View");
CGPoint origin = CGPointMake(20, 350);
CGRect rect2 = CGRectMake(origin.x, origin.y, 250, -250);
[AxesDrawer drawAxesInRect:rect2 originAtPoint:origin scale:[self scale]];

CGContextRef context = UIGraphicsGetCurrentContext();



UIGraphicsPushContext(context);

CGContextSetLineWidth(context, 2);
[[UIColor redColor] setStroke];


CGContextMoveToPoint(context, origin.x, origin.y);
for (CGFloat i=0; i<250; i++) {

    CGFloat yChange = [self.dataSource deltaY:self];

    CGContextAddLineToPoint(context, origin.x+i, origin.y-yChange);

    CGContextStrokePath(context);

}

UIGraphicsPopContext();

}

【问题讨论】:

    标签: ios core-graphics drawrect cs193p


    【解决方案1】:

    您必须将CGContextStrokePath(context); 放在for 循环之外。否则,它将在每次循环运行时创建一条新路径,并且失败。

    【讨论】:

      【解决方案2】:

      你有没有问题:

      CGRectMake(origin.x, origin.y, 250, -250)
      

      您指定一个负高度!

      【讨论】:

      • iOS 中的坐标系从左上角开始。因此正 x 值向右,正 y 值向下。因此,为了使一条线向上,我对 CGRectMake 的高度参数使用负 y 值
      • 如果您正在绘制线条,则为真,但此过程 (CGRectMake) 定义了一个矩形。
      • 谢谢,这实际上是一个重要的错误修复。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 2020-05-31
      • 2021-12-03
      • 2011-12-17
      • 2013-12-31
      相关资源
      最近更新 更多