Skip to content

Instantly share code, notes, and snippets.

@qy1010
Created January 8, 2020 06:32
Show Gist options
  • Save qy1010/481f33eb6d9f1b22aa189140e03b1eef to your computer and use it in GitHub Desktop.
Save qy1010/481f33eb6d9f1b22aa189140e03b1eef to your computer and use it in GitHub Desktop.
//镂空效果
- (void)photoFadeout
{
UIView *backgroundView = [[UIView alloc] init];
backgroundView.frame = self.view.bounds;
backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
[self.view addSubview:backgroundView];
// 创卷一个全屏大Path
UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.view.bounds];
// 创建一个圆形的Path
UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.view.center.x, self.view.center.y - 25)
radius:50
startAngle:0
endAngle:2 *M_PI
clockwise:NO];
[path appendPath:circlePath];
CAShapeLayer *sharpLayer = [CAShapeLayer layer];
sharpLayer.path = path.CGPath;
/*
  这个方法实现原理应该是path的FillRule 默认是FillRuleEvenOdd(CALayer 有一个fillRule属性的规则就有kCAFillRuleEvenOdd),
而EvenOdd 是一个奇偶规则,奇数则显示,偶数则不显示.叠加则是偶数故不显示.
*/
sharpLayer.fillRule = kCAFillRuleEvenOdd;
backgroundView.layer.mask = sharpLayer;
}
- (void)photoFadeout
{
UIView *backgroundView = [[UIView alloc] init];
backgroundView.frame = self.view.bounds;
backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
[self.view addSubview:backgroundView];
// 创卷一个全屏大Path
UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.view.bounds];
// 创建一个圆形的Path
UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.view.center.x, self.view.center.y - 25)
radius:50
startAngle:0
endAngle:2 *M_PI
clockwise:NO];
[path appendPath:circlePath];
CAShapeLayer *sharpLayer = [CAShapeLayer layer];
sharpLayer.path = path.CGPath;
/*
  这个方法实现原理应该是path的FillRule 默认是FillRuleEvenOdd(CALayer 有一个fillRule属性的规则就有kCAFillRuleEvenOdd),
而EvenOdd 是一个奇偶规则,奇数则显示,偶数则不显示.叠加则是偶数故不显示.
*/
sharpLayer.fillRule = kCAFillRuleEvenOdd;
backgroundView.layer.mask = sharpLayer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment