頭像選擇界面基本每個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];
}