How can I use Core Graphics to draw a circle at my touch location?

BendE picture BendE · Feb 16, 2013 · Viewed 15.1k times · Source

New programmer here. I am having issues trying to use Core Graphics to draw a stroked arc around my touch location. I have the method to draw the circle working fine, and I have tested and am registering touches when I tap the screen, but when I try to call the method to draw the circle when I tap, I get the error "CGContextBlahBlah: invalid context 0x0"

I think that this is because I'm not calling the method in drawRect:().

So how am I able to call this method on a touch? Furthermore, how can I use the "CGPoint locationOfTouch" as a parameter in my draw method?

Here are the code chunks I am working with.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint locationOfTouch = [touch locationInView:self];
    [self drawTouchCircle:(locationOfTouch)];
    [self setNeedsDisplay];
}


-(void)drawTouchCircle:(CGPoint)locationOfTouch
{
    CGContextRef ctx= UIGraphicsGetCurrentContext();

    CGContextSaveGState(ctx);

    CGContextSetLineWidth(ctx,5);
    CGContextSetRGBStrokeColor(ctx,0.8,0.8,0.8,1.0);
    CGContextAddArc(ctx,locationOfTouch.x,locationOfTouch.y,30,0.0,M_PI*2,YES);
    CGContextStrokePath(ctx);
}

Thanks in advance for the help!

Answer

Rob picture Rob · Feb 16, 2013

Yes, you're right. The issue is that, rather than calling drawTouchCircle yourself, you should implement a drawRect method that calls it for you, and thus your touches method only needs to call setNeedsDisplay, and drawRect will take care of the rest. You might, therefore, want to save the touch location in a class property, and then retrieve that in your drawRect:

@interface View ()
@property (nonatomic) BOOL touched;
@property (nonatomic) CGPoint locationOfTouch;
@end

@implementation View

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];

    self.touched = YES;
    UITouch *touch = [touches anyObject];
    self.locationOfTouch = [touch locationInView:self];
    [self setNeedsDisplay];
}

- (void)drawTouchCircle:(CGPoint)locationOfTouch
{
    CGContextRef ctx= UIGraphicsGetCurrentContext();
    CGRect bounds = [self bounds];

    CGPoint center;
    center.x = bounds.origin.x + bounds.size.width / 2.0;
    center.y = bounds.origin.y + bounds.size.height / 2.0;
    CGContextSaveGState(ctx);

    CGContextSetLineWidth(ctx,5);
    CGContextSetRGBStrokeColor(ctx,0.8,0.8,0.8,1.0);
    CGContextAddArc(ctx,locationOfTouch.x,locationOfTouch.y,30,0.0,M_PI*2,YES);
    CGContextStrokePath(ctx);
}

- (void)drawRect:(CGRect)rect
{
    if (self.touched)
        [self drawTouchCircle:self.locationOfTouch];
}

@end