美文网首页
遮罩的实现(镂空)裁剪

遮罩的实现(镂空)裁剪

作者: CodingTom | 来源:发表于2020-07-12 22:30 被阅读0次
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIColor *clearColor = [UIColor clearColor];

    CGFloat y = 254;
    CGFloat x = 100;

    CGContextAddArc(context, x, y, 40, 0, 2 * M_PI, 0); //添加一个圆

    CGContextSetFillColorWithColor(context, clearColor.CGColor);//填充颜色
    CGContextSetBlendMode(context, kCGBlendModeDestinationIn); //model 不同显示的效果不同
    CGContextDrawPath(context, kCGPathFillStroke); //绘制路径加填充
}
- (void)addMask{
    UIButton * _maskButton = [[UIButton alloc] init];
    [_maskButton setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [_maskButton setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.7]];
    [self.view addSubview:_maskButton];
    
    //create path
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    
    // MARK: circlePath
    [path appendPath:[UIBezierPath bezierPathWithArcCenter:CGPointMake(SCREEN_WIDTH / 2, 200) radius:100 startAngle:0 endAngle:2*M_PI clockwise:NO]];
    
    // MARK: roundRectanglePath
    [path appendPath:[[UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 400, SCREEN_WIDTH - 2 * 20, 100) cornerRadius:15] bezierPathByReversingPath]];
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    
    shapeLayer.path = path.CGPath;
    
    [_maskButton.layer setMask:shapeLayer];
}

相关连接

https://www.jianshu.com/p/7c92c57cdf6f

https://github.com/zhusongyu/ZSYMaskView

https://blog.csdn.net/chocolateloveme/article/details/17246887

https://blog.csdn.net/Dwarven/article/details/42492199

相关文章

网友评论

      本文标题:遮罩的实现(镂空)裁剪

      本文链接:https://www.haomeiwen.com/subject/lezncktx.html