Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ios尝试在图像上绘制点,然后通过线将它们连接起来_Ios_Objective C_Drawrect_Touchesbegan - Fatal编程技术网

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

ios尝试在图像上绘制点,然后通过线将它们连接起来,ios,objective-c,drawrect,touchesbegan,Ios,Objective C,Drawrect,Touchesbegan,我可以使用ToucheSStart中的代码在图像上绘制点。我将坐标存储在NSMutable数组中。我想让它在屏幕上标记点时画线。。但是我想我的drawRect没有开火。。你能告诉我怎么办吗 -(void) drawRect:(CGRect)rect { int *count = 0; if([pointarray count]!=0) { float firstpointx= [[pointarray objectAtIndex:0]floatValue

我可以使用ToucheSStart中的代码在图像上绘制点。我将坐标存储在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];
}
-(void)drawRect:(CGRect)rect
{
int*count=0;
如果([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];
//NSMUTABLEARRY*坐标=[[NSMUTABLEARRY alloc]init];

对于(int i=0;i您应该通过此方法调用
setNeedsDisplay

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

上触摸开始:withEvent:
您只需在
\u map
上调用
setNeedsDisplay
,而不是在
self
上,这就是视图不会被重新绘制的原因


另外,您只需添加两个点,但在
drawRect
中,如果确定数组包含四个点,您就可以进行编码。您可能要做的是在
touchSedend:withEvent
中添加两个点?如果是这样,您应该从那里调用
setNeedsDisplay

提示:为此,使用UIBezierPath更容易。您可能ean设置需要显示。