【问题标题】:iOS:Draw Multiple Lines Using CGContextAddLineToPointiOS:使用 CGContextAddLineToPoint 绘制多条线
【发布时间】:2012-09-17 11:46:28
【问题描述】:

我正在用drawrect写这段代码

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 10);
CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);


CGContextMoveToPoint(context, 502,530);
CGContextAddLineToPoint(context, x2, y2);


CGContextMoveToPoint(context, 502, 530);
CGContextAddLineToPoint(context, x3, y3);

CGContextMoveToPoint(context, 502, 530);
CGContextAddLineToPoint(context, x4, y4);

NSLog(@"%d,%d--%d,%d--%d,%d",x2,y2,x3,y3,x4,y4);



CGContextStrokePath(context);

但是这段代码总是只画出不正确的两行 它从不画第三条线 当我给出静态值而不是 x 和 y 代码时,三行代码可以正常工作 当我 NSLOG x 和 y 我得到正确的期望值 但不是根据那个画线 我想画4-5条坐标不断变化的线

请告诉我哪里出错了 或解决此问题的任何其他替代方法

【问题讨论】:

  • 是 x2、y2 等 CGFloat 值吗?
  • 它们是 int 值 Int 是否有效
  • 但是让它们 CGfloat 也行不通
  • 如果你画得更近,那么减小线宽并检查

标签: ios xcode cgcontext


【解决方案1】:

上面的代码运行良好,它们分别制作了三行(所有(502、530)的起点相同)。我认为你给两个点(如 x3,y3 或 x4,y4)的结束位置是相同的方向,这就是为什么你只得到两条线,如果你想区分这些线,你可以用不同颜色的线...

int x2, y2, x3, y3,x4, y4;
x2 = 100;
y2 = 100;
x3 = 200;
y3 = 200;
x4 = 200;
y4 = 50;

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 10);

// line 1
CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);    
CGContextMoveToPoint(context, 502,530);
CGContextAddLineToPoint(context, x2, y2);
CGContextStrokePath(context);

// line 2
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);    
CGContextMoveToPoint(context, 502, 530);
CGContextAddLineToPoint(context, x3, y3);
CGContextStrokePath(context);

// line 3
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);    
CGContextMoveToPoint(context, 502, 530);
CGContextAddLineToPoint(context, x4, y4);
CGContextStrokePath(context);

NSLog(@"%d,%d--%d,%d--%d,%d",x2,y2,x3,y3,x4,y4);

【讨论】:

  • 这也是我的情况结果我只画了两条线我检查了 x 和 y 的值是否正确且不重叠有没有其他方法可以绘制 3-4 条线
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-13
  • 2021-06-06
  • 2019-12-01
  • 2021-04-17
  • 2018-12-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多