【问题标题】:ios-Trying to draw points on image and then connect them through linesios-尝试在图像上绘制点,然后通过线将它们连接起来
【发布时间】:2014-03-20 21:40:05
【问题描述】:

我可以使用 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];
}

【问题讨论】:

  • 提示:为此目的更容易使用 UIBezierPath。

标签: ios objective-c drawrect touchesbegan


【解决方案1】:

touchedBegan:withEvent: 上,您只需在_map 上调用setNeedsDisplay,而不是在self 上,这就是视图不会重绘的原因。

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

【讨论】:

    【解决方案2】:

    你应该通过这个方法调用setNeedsDisplay

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

    【讨论】:

    • 你的意思可能是 setNeedsDisplay。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多