Wednesday, June 5, 2013

UIBezierPath bezierPathWithRoundedRect byRoundingCorners cornerRadii example in Objective C (iOS).


UIBezierPath bezierPathWithRoundedRect byRoundingCorners cornerRadii

Creates and returns a new UIBezierPath object initialized with a rounded rectangular path.

+ (UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii

Parameters
rect
The rectangle that defines the basic shape of the path.
corners
A bitmask value that identifies the corners that you want rounded. You can use this parameter to round only a subset of the corners of the rectangle.
cornerRadii
The radius of each corner oval. Values larger than half the rectangle’s width or height are clamped appropriately to half the width or height.

Return Value of [UIBezierPath bezierPathWithRoundedRect byRoundingCorners cornerRadii]
A new path object with the rounded rectangular path.

Discussion of [UIBezierPath bezierPathWithRoundedRect byRoundingCorners cornerRadii]
This method creates a closed subpath, proceeding in a clockwise direction (relative to the default coordinate system) as it creates the necessary line and curve segments.

UIBezierPath bezierPathWithRoundedRect byRoundingCorners cornerRadii example.
+ (CAShapeLayer *) roundedCornerOnImage: (UIImageView *)imageView onCorner: (UIRectCorner)rectCorner
{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds
                                                   byRoundingCorners:rectCorner
                                                         cornerRadii:CGSizeMake(10.0, 10.0)];

    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = imageView.bounds;
    maskLayer.path = maskPath.CGPath;

    return maskLayer;
}

Example of [UIBezierPath bezierPathWithRoundedRect byRoundingCorners cornerRadii].
+ (CAShapeLayer *) roundedCornerOnImage: (UIImageView *)imageView onCorner: (UIRectCorner)rectCorner
{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds
                                                   byRoundingCorners:rectCorner
                                                         cornerRadii:CGSizeMake(10.0, 10.0)];

    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = imageView.bounds;
    maskLayer.path = maskPath.CGPath;

    return maskLayer;
}

UIBezierPath bezierPathWithRoundedRect byRoundingCorners cornerRadii example.

-(CALayer*)maskForRoundedCorners:(UIRectCorner)corners withRadii:(CGSize)radii {
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = self.bounds;

    UIBezierPath *roundedPath = [UIBezierPath bezierPathWithRoundedRect:
        maskLayer.bounds byRoundingCorners:corners cornerRadii:radii];
    maskLayer.fillColor = [[UIColor whiteColor] CGColor];
    maskLayer.backgroundColor = [[UIColor clearColor] CGColor];
    maskLayer.path = [roundedPath CGPath];

    return maskLayer;
}

End of UIBezierPath bezierPathWithRoundedRect byRoundingCorners cornerRadii example article.