Wednesday, June 5, 2013

UIBezierPath closePath example in Objective C (iOS).


UIBezierPath closePath

Closes the most recently added subpath.

- (void)closePath

Discussion of [UIBezierPath closePath]
This method closes the current subpath by creating a line segment between the first and last points in the subpath. This method subsequently updates the current point to the end of the newly created line segment, which is also the first point in the now closed subpath.

UIBezierPath closePath example.
    UIBezierPath *aPath = [[UIBezierPath alloc] init];
        [aPath moveToPoint:CGPointMake(227,34.25)];
    [aPath addLineToPoint:CGPointMake(298.25,34.75)];
    [aPath addLineToPoint:CGPointMake(298.5,82.5)];
    [aPath addLineToPoint:CGPointMake(251,83)];
    [aPath addLineToPoint:CGPointMake(251,67.5)];
    [aPath addLineToPoint:CGPointMake(227.25,66.75)];  
        [aPath closePath];
    aPath.lineWidth = 2;
    [aPath fill];
    [aPath stroke];

Example of [UIBezierPath closePath].
    UIBezierPath*    aPath2 = [[UIBezierPath alloc] init];
    [aPath2 moveToPoint:CGPointMake(251.25,90.5)];
    [aPath2 addLineToPoint:CGPointMake(250.75,83.25)];
    [aPath2 addLineToPoint:CGPointMake(298.5,83)];
    [aPath2 addLineToPoint:CGPointMake(298.5,90.25)];
    [aPath2 closePath];
    aPath2.lineWidth = 2;
    [aPath2 fill];
    [aPath2 stroke];

UIBezierPath closePath example.
- (UIBezierPath *) createPath {
    static UIBezierPath *path = nil;
    if(!path) {
        path = [[UIBezierPath bezierPathWithOvalInRect:CGRectMake(35, 45, 250, 250)] retain];
        path.lineWidth = 50.0;
        [path closePath];
    }   
    return path;
}

End of UIBezierPath closePath example article.