Wednesday, June 5, 2013

UIBezierPath appendPath example in Objective C (iOS).


UIBezierPath appendPath

Appends the contents of the specified path object to the receiver’s path.

- (void)appendPath:(UIBezierPath *)bezierPath

Parameters
bezierPath
The path to add to the receiver.

Discussion of [UIBezierPath appendPath]
This method adds the commands used to create the path in bezierPath to the end of the receiver’s path. This method does not explicitly try to connect the subpaths in the two objects, although the operations in bezierPath might still cause that effect.

UIBezierPath appendPath example.
CGRect outerRect = {0, 0, 200, 200};
CGRect innerRect  = CGRectInset(outerRect,  30, 30);

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:outerRect cornerRadius:10];

[path appendPath:[UIBezierPath bezierPathWithRoundedRect:innerRect cornerRadius:5]];
path.usesEvenOddFillRule = YES;

[[UIColor orangeColor] set];
[path fill];

Example of [UIBezierPath appendPath].
CGRect rect1, rect2;
CGFloat radius;

  // fill in the values you want for the rects and the radius
UIBezierPath *result =
    [UIBezierPath bezierPathWithRoundedRect: rect1 cornerRadius:radius];
[result appendPath:
    [UIBezierPath bezierPathWithRoundedRect: rect2 cornerRadius:radius];

 // result is now a path comprising both of the roundrects.  You can fill it with a gradient like any other path.

UIBezierPath appendPath example.
- (void)makeCGPath
{
    UIBezierPath *path = [[UIBezierPath alloc] init];

    if (lines && lines.count>0)
    {
        for (int i = 0; i < lines.count; i++)
        {
            UIBezierPath *linePath = [[UIBezierPath alloc] init];

            NSArray *tempArray = [lines objectAtIndex:i];
            CGPoint p = [[tempArray objectAtIndex:0]CGPointValue];
            [linePath addLineToPoint:p];
            for (int j = 1; j < tempArray.count; j++)
            {
                p = [[tempArray objectAtIndex:j]CGPointValue];
                [linePath addLineToPoint:p];
            }
            [path appendPath:linePath];
        }
    }

    if (points && points.count > 0)
    {
        UIBezierPath *pointPath = [[UIBezierPath alloc] init];
        CGPoint p = [[points objectAtIndex:0]CGPointValue];
        [pointPath moveToPoint:p];
        for (int i = 1; i < points.count;i++ )
        {
            p = [[points objectAtIndex:i]CGPointValue];
            [pointPath moveToPoint:p];
        }
        [path appendPath:pointPath];
    }

    drawPath = path.CGPath;
    [self setNeedsDisplay];

    [lines removeAllObjects];
    [points removeAllObjects];
}

End of UIBezierPath appendPath example article.