Search code examples
uibezierpathuitouch

UIBezierPath colour


I followed this tutorial to create a drawing application. It all works really well at making smooth curves. The problem is that as I draw the line is black, and once I let go it makes the line the colour that I want. The tutorial does not go into line colour, but I have altered it. The only problem is that it's black until touchesEnded runs.

Here is my code:

This first section sets the colour to black.

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder])
    {
        [self setMultipleTouchEnabled:YES];
        path = [UIBezierPath bezierPath];
        [path setLineWidth:10.0];

        red = 0.0/255.0;
        green = 0.0/255.0;
        blue = 0.0/255.0;
        brush = 10.0;
        opacity = 0.8;

        toolSelected = 1;
        bgImage = 1;

    }
    return self;

}

Then on touchesEnded this code is ran:

- (void)drawBitmap
{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);

    if (!incrementalImage) // first time; paint background white
    {
        UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds];
        [[UIColor clearColor] setFill];
        [rectpath fill];
    }
    [incrementalImage drawAtPoint:CGPointZero];

    UIColor *colour = [UIColor colorWithRed:red green:green blue:blue alpha:opacity];

    [colour setStroke];
    [path stroke];
    incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

I assume that something needs to be ran in the touchesMoved to colourise the bezier path, but I'm just really struggling with it right now.

Can anyone help?
Thanks


Solution

  • It was achieved in the end by adding these two lines to this section

    - (void)drawRect:(CGRect)rect
    {
        [[UIColor redColor]setStroke];  // Added these
        [[UIColor redColor]setFill];    // two lines
        [incrementalImage drawInRect:rect];
        [path stroke];
    }