Skip to content

Instantly share code, notes, and snippets.

@rnystrom
Created February 27, 2013 23:04
Show Gist options
  • Save rnystrom/5052675 to your computer and use it in GitHub Desktop.
Save rnystrom/5052675 to your computer and use it in GitHub Desktop.
Masking a chip out of a UIView.
- (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