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
CGContextAddLineToPoint: no current point
提问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 CGContextMoveToPoint
was called before CGContextAddLineToPoint
get in action.
要获得当前的观点,您必须确保CGContextMoveToPoint
在CGContextAddLineToPoint
开始行动之前至少被调用过一次。
回答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 lastDot
has not yet been set. You need to explicitly set it to nil
first.
未初始化的自动变量是垃圾。您的代码在第一次通过循环时尝试做一些特别的事情,此时lastDot
尚未设置。您需要将其显式设置为nil
first。
回答by Rhythmic Fistman
You must first create a path with CGContextBeginPath
before 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。