Search code examples
iosobjective-cuibezierpath

How can I draw a dot on the screen on touchesEnded using UIBezierpath


Can someone here please show me how to draw a single dot using UIBezierpath? I am able to draw a line using the UIBezierpath but if I remove my finger and put it back and then remove nothing get drawn on the screen.

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];
    [pPath moveToPoint:p];
    [pPath stroke];
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
     [pPath stroke];
}

Solution

  • Your path doesn't include any line or curve segments to be stroked.

    Try this instead:

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        CGPoint p = [touches.anyObject locationInView:self];
        static CGFloat const kRadius = 3;
        CGRect rect = CGRectMake(p.x - kRadius, p.y - kRadius, 2 * kRadius, 2 * kRadius);
        pPath = [UIBezierPath bezierPathWithOvalInRect:rect];
        [self setNeedsDisplay];
    }
    
    - (void)drawRect:(CGRect)rect {
        [[UIColor blackColor] setFill];
        [pPath fill];
    }