Ios Can';t形直线
我试图在用户每次点击时连接线路。用户点击的点会被创建,但连接它们的线不会被创建。它给了我以下错误:Ios Can';t形直线,ios,objective-c,cocoa-touch,uiview,drawrect,Ios,Objective C,Cocoa Touch,Uiview,Drawrect,我试图在用户每次点击时连接线路。用户点击的点会被创建,但连接它们的线不会被创建。它给了我以下错误: CGContextAddLineToPoint: no current point. 谁能告诉我为什么?我检查了一下,没有发现任何相关的东西,因为我的笔划方法在最后。以下是我正在尝试的: - (id)initWithCoder:(NSCoder *)aDecoder { if (self = [super initWithCoder:aDecoder]) { [sel
CGContextAddLineToPoint: no current point.
谁能告诉我为什么?我检查了一下,没有发现任何相关的东西,因为我的笔划方法在最后。以下是我正在尝试的:
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self setMultipleTouchEnabled:NO];
[self setBackgroundColor:[UIColor clearColor]];
o = 0.5 / 1.0;
sW = 5.0;
r = 255.0/255.0;
g = 59.0/255.0;
b = 48.0/255.0;
first = CGPointZero;
second = CGPointZero;
third = CGPointZero;
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
[self setOpacity:o];
if (CGPointEqualToPoint(first, CGPointZero)) {
first = [touch locationInView:self];
}
else if (!CGPointEqualToPoint(first, CGPointZero) && CGPointEqualToPoint(second, CGPointZero)) {
second = [touch locationInView:self];
}
else if (!CGPointEqualToPoint(first, CGPointZero) && !(CGPointEqualToPoint(second, CGPointZero)) && CGPointEqualToPoint(third, CGPointZero)) {
third = [touch locationInView:self];
}
else {
first = CGPointZero;
second = CGPointZero;
third = CGPointZero;
}
[self setNeedsDisplay];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if (!CGPointEqualToPoint(first, CGPointZero) && CGPointEqualToPoint(second, CGPointZero)) {
first = [touch locationInView:self];
}
else if (!CGPointEqualToPoint(first, CGPointZero) && !(CGPointEqualToPoint(second, CGPointZero)) && CGPointEqualToPoint(third, CGPointZero)) {
second = [touch locationInView:self];
}
else {
third = [touch locationInView:self];
}
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self drawBitmap];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
}
- (void)drawRect:(CGRect)rect {
[incrementalImage drawInRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, sW);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), r, g, b, o);
NSLog(@"R:%f, G:%f, B:%f",r,g,b);
if (!CGPointEqualToPoint(first, CGPointZero)) {
CGRect rectangle = CGRectMake(first.x, first.y, 20, 20);
CGContextAddEllipseInRect(context, rectangle);
CGContextFillEllipseInRect(context, rectangle);
CGContextMoveToPoint(context, first.x, first.y);
}
if (!CGPointEqualToPoint(second, CGPointZero)) {
CGRect rectangle2 = CGRectMake(second.x, second.y, 20, 20);
CGContextAddEllipseInRect(context, rectangle2);
CGContextFillEllipseInRect(context, rectangle2);
CGContextAddLineToPoint(context, second.x, second.y);
}
if (!CGPointEqualToPoint(third, CGPointZero)) {
CGRect rectangle3 = CGRectMake(third.x, third.y, 20, 20);
CGContextAddEllipseInRect(context, rectangle3);
CGContextFillEllipseInRect(context, rectangle3);
CGContextAddLineToPoint(context, third.x, third.y);
}
CGContextStrokePath(context);
CGContextDrawPath(context, kCGPathFill);
}
- (void)drawBitmap {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
[[UIColor redColor] setStroke];
//CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 255.0/255.0, 255.0/255.0, 255.0/255.0, 1.0/1.0);
if (!incrementalImage) { // first draw;
UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds]; // enclosing bitmap by a rectangle defined by another UIBezierPath object
[[UIColor clearColor] setFill];
[rectpath fill]; // fill it
}
[incrementalImage drawAtPoint:CGPointZero];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, sW);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), r, g, b, o);
if (!CGPointEqualToPoint(first, CGPointZero)) {
CGRect rectangle = CGRectMake(first.x, first.y, 20, 20);
CGContextAddEllipseInRect(context, rectangle);
CGContextFillEllipseInRect(context, rectangle);
CGContextMoveToPoint(context, first.x, first.y);
}
if (!CGPointEqualToPoint(second, CGPointZero)) {
CGRect rectangle2 = CGRectMake(second.x, second.y, 20, 20);
CGContextAddEllipseInRect(context, rectangle2);
CGContextFillEllipseInRect(context, rectangle2);
CGContextAddLineToPoint(context, second.x, second.y);
}
if (!CGPointEqualToPoint(third, CGPointZero)) {
CGRect rectangle3 = CGRectMake(third.x, third.y, 20, 20);
CGContextAddEllipseInRect(context, rectangle3);
CGContextFillEllipseInRect(context, rectangle3);
CGContextAddLineToPoint(context, third.x, third.y);
}
CGContextStrokePath(context);
incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
此外,笔划颜色始终为黑色。我把它记录下来,它说明了正确的颜色。“无当前点”错误没有意义的是,我在点击时使用了相同的参数来创建圆,而且效果很好,唯一不起作用的是线条。这里您调用的是第三个点,但它应该是第二个点
if(!CGPointEqualToPoint(third, CGPointZero)){
CGContextAddLineToPoint(context, second.x, second.y);
}
CGContextAddEllipseInRect(context, rectangle3);
CGContextFillEllipseInRect(context, rectangle3);
你在这里再次要求第三点,但是在适当的地方
if(!CGPointEqualToPoint(third, CGPointZero)){
CGContextAddLineToPoint(context, third.x, third.y);
}
谢谢,这是一个错误,但不是上面列出的错误的原因。np,只是我注意到了一些东西。不幸的是,我们不能确定错误。哪一行代码导致了错误消息?如果您只是想画线,UIBezierPath可能更容易使用。(看起来更好)@Cutetare:我只是想画线,但我尝试了UIBezierPath,但也没有成功代码>中风后path@Cutetare:无:(,我添加了我所有的代码,其中有任何代码有帮助吗?