xcode CGContextAddLineToPoint: 没有当前点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9799682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:44:17  来源:igfitidea点击:

CGContextAddLineToPoint: no current point

iphonexcodeios5xcode4xcode4.2

提问by Pardhu

I am developing a pattern lock application, (like the Android lock).

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

I want to draw lines between the points to open the lock, but when I am drawing, it returns an error:

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

<Error>: CGContextAddLineToPoint: no current point

<Error>: CGContextAddLineToPoint: no current point

It's working fine in iOS 5.0 and before but it's showing an error in 5.1.

它在 iOS 5.0 及更早版本中运行良好,但在 5.1 中显示错误。

This is my code:

这是我的代码:

 - (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
 }

回答by bucherland

To have current point, You have to ensure that at least once CGContextMoveToPointwas called before CGContextAddLineToPointget in action.

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

回答by Kurt Revis

This:

这个:

UIView *lastDot;

Should be:

应该:

UIView *lastDot = nil;

Uninitialized automatic variables are garbage. Your code is trying to do something special the first time through the loop, when lastDothas not yet been set. You need to explicitly set it to nilfirst.

未初始化的自动变量是垃圾。您的代码在第一次通过循环时尝试做一些特别的事情,此时lastDot尚未设置。您需要将其显式设置为nilfirst。

回答by Rhythmic Fistman

You must first create a path with CGContextBeginPathbefore you can start adding points and lines to it.

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

回答by lidechao

When you first come, the forin method won't come in.So there are far use of the method "CGContextMoveToPoint()" , and then result in the warning.

刚来的时候,forin方法是不会进来的,所以“CGContextMoveToPoint()”这个方法用的比较多,然后导致warning。