ios-попытка нарисовать точки на изображении, а затем соединить их линиями

я могу рисовать точки на своем изображении, используя код в 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];
}

person user3252870    schedule 20.03.2014    source источник
comment
Совет: для этого проще использовать UIBezierPath.   -  person Ramy Al Zuhouri    schedule 21.03.2014


Ответы (2)


На touchedBegan:withEvent: вы просто вызываете setNeedsDisplay на _map, а не на self, поэтому вид не перерисовывается.

Кроме того, вы просто добавляете две точки, но в drawRect вы кодируете так, будто уверены, что массив содержит четыре точки. Вероятно, вы хотите добавить две точки в touchesEnded:withEvent? Если это так, вы должны позвонить setNeedsDisplay оттуда.

person Ramy Al Zuhouri    schedule 20.03.2014

Вы должны вызвать setNeedsDisplay из этого метода:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
}
person Matteo Gobbi    schedule 20.03.2014
comment
Вы, вероятно, имеете в виду setNeedsDisplay. - person Ramy Al Zuhouri; 21.03.2014