Created
February 27, 2013 23:04
-
-
Save rnystrom/5052675 to your computer and use it in GitHub Desktop.
Masking a chip out of a UIView.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)maskChip { | |
UIGraphicsBeginImageContextWithOptions(self.containerView.bounds.size, NO, [UIScreen mainScreen].scale); | |
CGContextRef ctx = UIGraphicsGetCurrentContext(); | |
CGRect frame = self.containerView.bounds; | |
CGMutablePathRef path = CGPathCreateMutable(); | |
CGPathMoveToPoint(path, NULL, 0, 0); | |
CGPathAddLineToPoint(path, NULL, frame.size.width / 2.f - 6, 0); | |
CGPathAddLineToPoint(path, NULL, frame.size.width / 2.f, 6); | |
CGPathAddLineToPoint(path, NULL, frame.size.width / 2.f + 6, 0); | |
CGPathAddLineToPoint(path, NULL, frame.size.width, 0); | |
CGPathAddLineToPoint(path, NULL, frame.size.width, self.view.bounds.size.height); | |
CGPathAddLineToPoint(path, NULL, 0, frame.size.height); | |
CGPathAddLineToPoint(path, NULL, 0, 0); | |
CGPathCloseSubpath(path); | |
CGContextAddPath(ctx, path); | |
CGContextSetFillColorWithColor(ctx, [UIColor blackColor].CGColor); | |
CGContextFillPath(ctx); | |
UIImage *maskImg = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
CGPathRelease(path); | |
CAShapeLayer *layer = [CAShapeLayer layer]; | |
layer.contents = (id)maskImg.CGImage; | |
layer.frame = frame; | |
self.containerView.layer.mask = layer; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment