1

I have a basic map view in my app which contains a set of user definable way points. When the view is loaded I draw a path connecting the way points. This works well.

However, when the user drags a way point around the view, I would like it to re-draw the paths and that is my problem. I dont know how to hacant get it to draw anything after the first time. Here's my code:

- (void)drawRect:(CGRect)rect { ////// works perfectly

    context = UIGraphicsGetCurrentContext();

    CGContextSetRGBStrokeColor(context, 1, 0, 1, .7);
    CGContextSetLineWidth(context, 20.0);
    WaypointViewController *w = [arrayOfWaypoints objectAtIndex:0];
    CGPoint startPoint = w.view.center;

    CGContextMoveToPoint(context, startPoint.x, startPoint.y);

    for (int i = 1; i<[arrayOfWaypoints count]; i++) {
        WaypointViewController *w2 = [arrayOfWaypoints objectAtIndex:i];
        CGPoint nextPoint = w2.view.center;
        CGContextAddLineToPoint(context,nextPoint.x, nextPoint.y);
    }
    CGContextStrokePath(context);
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    if (moving) {
        UITouch *touch = [touches anyObject];
        currentWaypoint.view.center = [touch locationInView:self];
        [delegate setUserInteraction:NO];
        [self drawInContext:context];
        [NSThread detachNewThreadSelector:@selector(drawInContext:) toTarget:self withObject:nil];

    }

}

- (void)drawInContext:(CGContextRef)context { ///gets called, but does nothing

    CGContextSetRGBStrokeColor(context, 1, 0, 1, .7);
    CGContextSetLineWidth(context, 20.0);
    WaypointViewController *w = [arrayOfWaypoints objectAtIndex:0];
    CGPoint startPoint = w.view.center;

    CGContextMoveToPoint(context, startPoint.x, startPoint.y);

    for (int i = 1; i<[arrayOfWaypoints count]; i++) {
        WaypointViewController *w2 = [arrayOfWaypoints objectAtIndex:i];
        CGPoint nextPoint = w2.view.center;
        CGContextAddLineToPoint(context,nextPoint.x, nextPoint.y);
    }

    CGContextStrokePath(context);
}

3 Answers 3

4

You can not use the context that is active in drawRect: anywhere else.

Instead of calling [self drawInContext:context] call [self setNeedsDisplay]

The context that you are pointing to in your drawRect: might be freed or might still be active you don't know, but either way there is no way to get the bits you put into that context onto the screen.

Also, please read the docs on drawing on iOS here

2

You may not save the context for use after drawRect: returns. What you need to do is call [self setNeedsDisplay] (or setNeedsDisplayInRect:, if you can determine the bounding rectangle of the change), which will cause the system to call drawRect: again for you.

0

You can do all your drawing only in drawRect ...Hope this helps ... hence - (void)drawInContext:(CGContextRef)context will not be able to do any drawing. if after user's touch action , you need to do some redrawing then you need to call [self setNeedsDisplay] in your touchmethods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.