Skip to content

Instantly share code, notes, and snippets.

@0xMarK
Created April 15, 2015 22:46
Show Gist options
  • Save 0xMarK/08effddac69f502fcece to your computer and use it in GitHub Desktop.
Save 0xMarK/08effddac69f502fcece to your computer and use it in GitHub Desktop.
Mask a hole inside a rect.
// Mask
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.fillRule = kCAFillRuleEvenOdd;
maskLayer.frame = CGRectMake(0, 0, self.notificationsTabBarButton.frame.size.width, self.notificationsTabBarButton.frame.size.height);
CGPoint circleCenter = self.notificationsBadge.center;
CGFloat innerCircleRadius = self.notificationsBadge.frame.size.width/2;
CGFloat circleRadius = innerCircleRadius + 2;
UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:circleCenter radius:circleRadius startAngle:0.0 endAngle:M_PI * 2.0 clockwise:YES];
UIBezierPath *innerCirclePath = [UIBezierPath bezierPathWithArcCenter:circleCenter radius:innerCircleRadius startAngle:0.0 endAngle:M_PI * 2.0 clockwise:YES];
UIBezierPath *path = [UIBezierPath bezierPathWithRect:maskLayer.frame];
path.usesEvenOddFillRule = YES;
[path appendPath:circlePath];
[path appendPath:innerCirclePath];
maskLayer.path = path.CGPath;
self.notificationsTabBarButton.layer.mask = maskLayer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment