Wednesday, June 5, 2013

UIBezierPath bezierPath example in Objective C (iOS).


UIBezierPath bezierPath

Creates and returns a new UIBezierPath object.

+ (UIBezierPath *)bezierPath

Return Value of [UIBezierPath bezierPath]
A new empty path object.

UIBezierPath bezierPath example.
    UIBezierPath*    aPath = [UIBezierPath bezierPath];

    [aPath moveToPoint:CGPointMake(200.053,79.688)];   
    [aPath addLineToPoint:CGPointMake(100.053,179.688)];
    [aPath addLineToPoint:CGPointMake(304.412,280.125)];
    [aPath addLineToPoint:CGPointMake(308.055,298.513)];
    [aPath addLineToPoint:CGPointMake(200.053,79.688)];

    [aPath closePath];

    [[UIColor blackColor] setStroke];
    [[UIColor redColor] setFill];

    CGContextRef aRef = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(aRef, 50, 50);
    aPath.lineWidth = 5;
    [aPath fill]; 

Example of [UIBezierPath bezierPath].
UIBezierPath *aPath = [UIBezierPath bezierPath];

    // Set the starting point of the shape.
    [aPath moveToPoint:CGPointMake(50.0, 50.0)];
    // Draw the lines.
    [aPath addLineToPoint:CGPointMake(100.0, 50.0)];
    [aPath addLineToPoint:CGPointMake(100.0, 100.0)];
    [aPath addLineToPoint:CGPointMake(50.0, 100.0)];
    [aPath closePath];

    CAShapeLayer *square = [CAShapeLayer layer];
    square.path = aPath.CGPath;
    [self.view.layer addSublayer:square];

UIBezierPath bezierPath example.
UIBezierPath *aPath = [UIBezierPath bezierPath];
    // Set the starting point of the shape.
    [aPath moveToPoint:CGPointMake(0.0, 0.0)];
    // Draw the lines.
    [aPath addLineToPoint:CGPointMake(50.0, 0.0)];
    [aPath addLineToPoint:CGPointMake(50.0, 50.0)];
    [aPath addLineToPoint:CGPointMake(0.0, 50.0)];
    [aPath closePath];

    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bar.png"]];
    imgView.frame = CGRectMake(0, 0, 100, 100);
    [self setClippingPath:aPath :imgView];
    [self.view addSubview:imgView];

End of UIBezierPath bezierPath example article.