CGContextAddLineToPoint:没有当前点

Par*_*dhu 9 iphone xcode xcode4 ios5 xcode4.2

我正在开发一个模式锁定应用程序(如Android锁定).

我想在点之间画线来打开锁,但是当我画画时,它会返回一个错误:

<Error>: CGContextAddLineToPoint: no current point

它在iOS 5.0和之前的工作正常但它在5.1中显示错误.

这是我的代码:

 - (void)drawRect:(CGRect)rect
{
 NSLog(@"drawrect...%@",NSStringFromCGRect(rect));

 if (!self._trackPointValue)
 return;

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 10.0);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {0.5, 1.0, 0.5, 0.8};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);

CGPoint from;
UIView *lastDot;
for (UIView *dotView in self._dotViews) {  //_dotViews array of points
 from = dotView.center;      
 if (!lastDot)
 {
  CGContextMoveToPoint(context, from.x, from.y);

  }
 else
 {
    NSLog(@"from : %@",NSStringFromCGPoint(from));
   CGContextAddLineToPoint(context, from.x, from.y);

 }

 lastDot = dotView;
}

 CGPoint pt = [self._trackPointValue CGPointValue];  //_trackPointValue is current point

 CGContextAddLineToPoint(context, pt.x, pt.y);

 CGContextStrokePath(context);
 CGColorSpaceRelease(colorspace);
 CGColorRelease(color);

 self._trackPointValue = nil;//_trackPointValue is current point
 }
Run Code Online (Sandbox Code Playgroud)

buc*_*and 13

要获得当前的观点,您必须确保CGContextMoveToPointCGContextAddLineToPoint开始行动之前至少调用过一次.


Rhy*_*man 1

您必须先创建一条路径,CGContextBeginPath然后才能开始向其中添加点和线。

  • 不对。CGContext 始终有一个当前路径(可能是空路径),您可以向其中添加元素。仅当您想放弃当前路径并从新的空路径开始时,才需要“CGContextBeginPath”。 (4认同)