gpt4 book ai didi

ios-尝试在图像上绘制点,然后通过线连接它们

转载 作者:行者123 更新时间:2023-11-28 22:12:29 24 4
gpt4 key购买 nike

我可以使用 touchesbegan 中的代码在图像上绘制点。我将我的坐标存储在 NSMutable 数组中。我希望它在屏幕上标记点时画线..但我猜我的 drawRect 没有触发..你能告诉我该怎么做吗..

-(void) drawRect:(CGRect)rect
{

int *count = 0;
if([pointarray count]!=0)
{
float firstpointx= [[pointarray objectAtIndex:0]floatValue];
float firstpointy= [[pointarray objectAtIndex:1]floatValue];
float secondpointx= [[pointarray objectAtIndex:2]floatValue];
float secondpointy= [[pointarray objectAtIndex:3]floatValue];

//NSMutableArray *coordinates = [[NSMutableArray alloc] init];
for (int i=0; i<=[pointarray count]; i++) {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
CGContextSetLineWidth(ctx, 2.0);
CGContextMoveToPoint(ctx, firstpointx, firstpointy);///move to ur first dot
CGContextAddLineToPoint(ctx, secondpointx, secondpointy);//add line from first dot to second dot

CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextStrokePath(ctx);
[pointarray removeAllObjects];//remove first two points from ur array so that next line is not drawn in continuous with previous line
}

count++;

}
}



-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

pointarray=[[NSMutableArray alloc]init];

CGPoint curPoint = [[touches anyObject] locationInView:self.map];
[pointarray addObject:[NSNumber numberWithFloat:curPoint.x]];
[pointarray addObject:[NSNumber numberWithFloat:curPoint.y]];
[_map setNeedsDisplay];

[self logMessage:[NSString stringWithFormat:@"Sending : %@", pointarray]];
NSLog(@"the point array is %@",pointarray);
NSArray *coordinates = [[NSArray alloc]initWithArray:pointarray copyItems:YES];
NSLog(@"the coordinate array %@",coordinates);

//[self.map setNeedsDisplay]; // calls drawRectMethod

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(curPoint.x, curPoint.y, 10, 10)];
view.backgroundColor = [UIColor redColor];
[self.map addSubview:view];
}

最佳答案

touchedBegan:withEvent: 上,您只需在 _map 上调用 setNeedsDisplay,而不是在 self 上,这就是为什么 View 不会重绘。

此外,您只需添加两个点,但在 drawRect 中,如果确定数组包含四个点,您的代码就像这样。可能你想要做的是在touchesEnded:withEvent中添加两个点?如果是这样,您应该从那里调用 setNeedsDisplay

关于ios-尝试在图像上绘制点,然后通过线连接它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22545701/

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