Wednesday, June 5, 2013

UIBezierPath UIRectCornerAllCorners example in Objective C (iOS).


UIBezierPath UIRectCornerAllCorners

UIRectCorner
The corners of a rectangle.

enum {
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0
};
typedef NSUInteger UIRectCorner;

Constants
UIRectCornerTopLeft
The top-left corner of the rectangle.
UIRectCornerTopRight
The top-right corner of the rectangle.
UIRectCornerBottomLeft
The bottom-left corner of the rectangle.
UIRectCornerBottomRight
The bottom-right corner of the rectangle.
UIRectCornerAllCorners
All corners of the rectangle.

Discussion of [UIBezierPath UIRectCornerAllCorners]
The specified constants reflect the corners of a rectangle that has not been modified by an affine transform and is drawn in the default coordinate system (where the origin is in the upper-left corner and positive values extend down and to the right).

UIBezierPath UIRectCornerAllCorners example.
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:frame
                                                           byRoundingCorners:UIRectCornerAllCorners
                                                                 cornerRadii:CGSizeMake(5.0, 5.0)];
            // Create the shape layer and set its path
            CAShapeLayer *maskLayer = [CAShapeLayer layer];
            maskLayer.frame = frame;
            maskLayer.path = maskPath.CGPath;
            // Set the newly created shape layer as the mask for the image view's layer
            imageView.layer.mask = maskLayer;
imageView.layer.cornerRadius = 5.0;

Example of [UIBezierPath UIRectCornerAllCorners].
- (void)drawRect:(CGRect)rect
{
    self.layer.masksToBounds = NO;
    self.layer.shadowColor = [[UIColor xxxx] CGColor];
    self.layer.shadowOffset = CGSizeMake(0,2);
    self.layer.shadowRadius = ...
    self.layer.shadowOpacity = ...

    UIBezierPath *path = [UIBezierPath bezierPathWithPartiallyRoundedRect:rect
           byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(20, 20)];
    [[UIColor blackColor] setFill or Stroke];

    [path stroke or fill];
}

UIBezierPath UIRectCornerAllCorners example.
- (void) layoutSubviews {
    [super layoutSubviews];

    const CGFloat PageCellBackgroundRadius = 6.0;

    for(int i = 0; i < [self numberOfSections]; i++) {
        NSInteger viewTag = i + 123456;
        CGRect frameRect = [self shadowFrameForSection: i];

        UIView* shadowBackgroundView = [self viewWithTag: viewTag];
        if (shadowBackgroundView) {
            if (!CGRectEqualToRect(frameRect, shadowBackgroundView.frame)) {
                shadowBackgroundView.frame = frameRect;
                CGPathRef shadowPath = [UIBezierPath bezierPathWithRoundedRect: shadowBackgroundView.bounds
                                                             byRoundingCorners: UIRectCornerAllCorners
                                                                   cornerRadii: CGSizeMake(PageCellBackgroundRadius, PageCellBackgroundRadius)].CGPath;
                shadowBackgroundView.layer.shadowPath = shadowPath;
            }

            [self sendSubviewToBack: shadowBackgroundView];
        } else {
            shadowBackgroundView = [[[UIView alloc] initWithFrame: frameRect] autorelease];
            shadowBackgroundView.tag = viewTag;
            shadowBackgroundView.opaque = YES;
            shadowBackgroundView.backgroundColor = [UIColor clearColor];

            shadowBackgroundView.layer.shadowOpacity = 0.3;
            shadowBackgroundView.layer.shadowRadius = 2;
            shadowBackgroundView.layer.shadowColor = [[UIColor blackColor] CGColor];
            shadowBackgroundView.layer.shadowOffset = CGSizeMake(0.0, 1.0);
            CGPathRef shadowPath = [UIBezierPath bezierPathWithRoundedRect: shadowBackgroundView.bounds
                                                         byRoundingCorners: UIRectCornerAllCorners
                                                               cornerRadii: CGSizeMake(PageCellBackgroundRadius, PageCellBackgroundRadius)].CGPath;
            shadowBackgroundView.layer.shadowPath = shadowPath;
            shadowBackgroundView.layer.shouldRasterize = YES;

            [self addSubview: shadowBackgroundView];
        }
    }
}

End of UIBezierPath UIRectCornerAllCorners example article.