gpt4 book ai didi

ios - 画不出直线

转载 作者:行者123 更新时间:2023-11-29 03:27:57 26 4
gpt4 key购买 nike

我试图在用户每次点击时连接线路。用户点击的点会被创建,但连接它们的线不会被创建。它给了我以下错误:

CGContextAddLineToPoint: no current point.

谁能告诉我为什么?我检查了SO,但找不到任何相关内容,因为我的笔划方法位于最后。这是我正在尝试的:

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

关于ios - 画不出直线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20206493/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com