在一个CGPoint和另一个CGPoint之间画一条线

Jos*_*hua 1 iphone core-graphics

我有2个CGPoints,并且想在它们之间绘制直线,我该怎么做?

dan*_*aro 6

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(ctx, point1.x, point1.y);
CGContextAddLineToPoint(ctx, point2.x, point2.y);
CGContextStrokePath(ctx);
Run Code Online (Sandbox Code Playgroud)

  • 约书亚:好吧,看看他们说的话.上下文无效的最可能原因是您没有将此代码粘贴到正确的位置.尝试将代码放入一个有效的当前绘图上下文的方法中. (3认同)
  • -touchesEnded:不是您拥有有效当前绘图上下文的位置.彼得所指的是像-drawRect:自定义UIView子类的某个地方.然后,您将使用-setNeedsDisplay触发重绘视图.UIView类参考有更多:http://developer.apple.com/iPhone/library/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/drawRect: (3认同)