效果图
mask属性可以用来做遮罩层、蒙版类似于PS;
//全局属性
@property(nonatomic,strong) CAShapeLayer *layerShape;
@property(nonatomic,strong) CAShapeLayer *layerCircle;
//实现代码
- (void)maskLayterTest {
CGRect frame = CGRectMake(0, 0, 200, 200);
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:frame];
CALayer *layerBG = [CALayer layer];
layerBG.backgroundColor = [UIColor yellowColor].CGColor;
layerBG.bounds = frame;
layerBG.position = self.view.center;
[self.view.layer addSublayer:layerBG];
_layerShape = [CAShapeLayer layer];
_layerShape.bounds = frame;
_layerShape.path = path.CGPath;
_layerShape.fillColor = [UIColor greenColor].CGColor;
_layerShape.position = CGPointMake(100, 100);
[layerBG addSublayer:_layerShape];
CGRect frameB = CGRectMake(0, 0, 200, 200);
UIBezierPath *pathB = [UIBezierPath bezierPathWithOvalInRect:frameB];
_layerCircle = [CAShapeLayer layer];
_layerCircle.bounds = frameB;
_layerCircle.lineWidth = 120;
_layerCircle.strokeColor = [UIColor redColor].CGColor;
_layerCircle.path = pathB.CGPath;
_layerCircle.strokeEnd = 0.2;
_layerCircle.fillColor = [UIColor clearColor].CGColor;
_layerCircle.position = CGPointMake(100, 100);
//这里可以不加到layerBG
[layerBG addSublayer:_layerCircle];
_layerShape.mask = _layerCircle;
}