Domanda

I would like to mention that this post is similar to Continuous drawing in CGContext with drawRect but it didn't have any code snippet solution so asking again.

I am learning iOS development application. I am trying to make a drawing application using UIBezierPath. For the moment to whenever I have a new touch and a new UIBezierPath I do the following to show my previous UIBezierPath. Please let me know if there is better/recommended way. I have color array to keep track of color used to draw each beizer path as I have option of changing color for each path.

- (void)drawRect:(CGRect)rect // (4)
{
    UIBezierPath *currentPath;
    UIColor *currentColor;
    for (int index=0;index<[self.pathArray count]; index++)
    {
        currentPath = [self.pathArray objectAtIndex:index];
        currentColor = [self.strokeArray objectAtIndex:index];
        [currentColor setStroke];
        [currentPath stroke];
    }
}


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


    UIColor *currentStrokeColor;
    currentStrokeColor = [self copyColor:self.strokeColor];
    self.path = [UIBezierPath bezierPath];
    [self.path setLineWidth:2.0];
    [self.pathArray addObject:self.path];
    [self.strokeArray addObject:currentStrokeColor];
    [self.path moveToPoint:p];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];
    [self.path addLineToPoint:p]; // (4)
    [self setNeedsDisplay];
}
È stato utile?

Soluzione

After drawing a path you can render it to an image. That way each path is concatenated into a single image that will be far more efficient for drawing. Please see this question for code snippets.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top