iOS开发-绘制

iOS 透明遮罩层

2017-06-09  本文已影响976人  Jesscia_Liu
#define MLScreenH [UIScreen mainScreen].bounds.size.height
#define MLScreenW [UIScreen mainScreen].bounds.size.width

@implementation GuideView
- (instancetype)init{
    self = [super init];
    self.frame = CGRectMake(0, 0, MLScreenW, MLScreenH);
    self.backgroundColor=[UIColor clearColor];
    return self;
}

- (void)drawRect:(CGRect)rect {
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    if (context == nil) {
        return;
    }
    
    [[[UIColor blackColor] colorWithAlphaComponent:0.8f] setFill];
    UIRectFill(rect);
    
    [[UIColor clearColor] setFill];
    
    //设置透明部分位置和圆角
    CGRect alphaRect = CGRectMake(10, 10, 100, 100);
    CGFloat cornerRadius = 10;
    UIBezierPath *bezierPath=[UIBezierPath bezierPathWithRoundedRect:alphaRect
                                                        cornerRadius:cornerRadius];
    CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor clearColor] CGColor]);
    CGContextAddPath(UIGraphicsGetCurrentContext(), bezierPath.CGPath);
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
    CGContextFillPath(UIGraphicsGetCurrentContext());
    
}
resultImage.png
上一篇 下一篇

猜你喜欢

热点阅读