頭像選擇界面:周圍灰暗,中間透明的效果

頭像選擇界面基本每個App都會有,近來重新整理了下它的實現(xiàn)方式。圖示效果代碼在文末。

image.png

1、使用UIGraphic

重寫遮罩視圖的drawRect

- (void)drawRect:(CGRect)rect {
    
    //背景
    [[[UIColor blackColor] colorWithAlphaComponent:.6] set];
    UIRectFill(rect);
    
    //鏤空
    [[UIColor clearColor] set];
    CGRect holeRect = CGRectMake(100, 100, 200, 200);
    UIRectFill(holeRect);
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

2、使用UIBezierPath和CAShapeLayer

重寫遮罩視圖的drawRect

- (void)drawRect:(CGRect)rect {
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect];

    //設(shè)置path
    CGRect myRect = CGRectMake(100,100,200, 200);
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:myRect];
    [path appendPath:circlePath];
    [path setUsesEvenOddFillRule:YES];

    //填充內(nèi)容
    CAShapeLayer *fillLayer = [CAShapeLayer layer];
    fillLayer.path = path.CGPath;
    fillLayer.fillRule = kCAFillRuleEvenOdd;
    fillLayer.fillColor = [[UIColor blackColor] colorWithAlphaComponent:.6].CGColor;
    [self.layer addSublayer:fillLayer];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

或在應(yīng)用時直接添加layer

- (void)addMaskLayer {
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.view.bounds];
    
    //設(shè)置path
    CGRect myRect = CGRectMake(100,100,200, 200);
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:myRect];
    [path appendPath:circlePath];
    [path setUsesEvenOddFillRule:YES];
    
    //填充內(nèi)容
    CAShapeLayer *fillLayer = [CAShapeLayer layer];
    fillLayer.path = path.CGPath;
    fillLayer.fillRule = kCAFillRuleEvenOdd;
    fillLayer.fillColor = [[UIColor blackColor] colorWithAlphaComponent:.6].CGColor;
    [self.view.layer addSublayer:fillLayer];
}

3、使用CoreGraphic

重寫遮罩視圖的drawRect

- (void)drawRect:(CGRect)rect {
    //背景
    CGContextRef context = UIGraphicsGetCurrentContext();
    [[[UIColor blackColor] colorWithAlphaComponent:.6] set];
    CGContextAddRect(context, rect);
    CGContextFillPath(context);

    //鏤空
    CGRect circleRect = CGRectMake(100, 100, 200, 200);
    CGContextSetBlendMode(context, kCGBlendModeClear);
    CGContextAddEllipseInRect(context, circleRect);
    CGContextFillPath(context);
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

4、示例代碼

- (void)avatarOverlayer {
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:[UIScreen mainScreen].bounds];
    
    //黑色背景
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:self.cropFrame];
    [path appendPath:circlePath];
    [path setUsesEvenOddFillRule:YES];
    
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.path = path.CGPath;
    maskLayer.fillRule = kCAFillRuleEvenOdd;
    maskLayer.fillColor = [[UIColor blackColor] colorWithAlphaComponent:0.6].CGColor;
    [self.overlayView.layer addSublayer:maskLayer];
    
    //白色邊框
    UIBezierPath *strokePath = [UIBezierPath bezierPathWithOvalInRect:self.cropFrame];
    [strokePath stroke];
    CAShapeLayer *strokeLayer = [CAShapeLayer layer];
    strokeLayer.strokeColor = [UIColor whiteColor].CGColor;
    strokeLayer.fillColor = [UIColor clearColor].CGColor;
    strokeLayer.path = strokePath.CGPath;
    [self.overlayView.layer addSublayer:strokeLayer];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容