Is there a way to prevent CALayer shadows from overlapping adjacent layers?

时光总嘲笑我的痴心妄想 提交于 2019-12-03 08:10:25

If all of the shadowed layers have the same shadow settings, put them into a container layer and set the shadow on the container layer. Example:

- (void)viewDidLoad
{
    [super viewDidLoad];

    CALayer *containerLayer = [CALayer layer];
    containerLayer.frame = self.view.bounds;
    containerLayer.shadowRadius = 10;
    containerLayer.shadowOpacity = 1;
    [self.view.layer addSublayer:containerLayer];

    CAShapeLayer *layer1 = [CAShapeLayer layer];
    layer1.bounds = CGRectMake(0, 0, 200, 200);
    layer1.position = CGPointMake(130, 130);
    layer1.path = [UIBezierPath bezierPathWithOvalInRect:layer1.bounds].CGPath;
    layer1.fillColor = [UIColor redColor].CGColor;
    [containerLayer addSublayer:layer1];

    CAShapeLayer *layer2 = [CAShapeLayer layer];
    layer2.bounds = CGRectMake(0, 0, 200, 200);
    layer2.position = CGPointMake(170, 200);
    layer2.path = [UIBezierPath bezierPathWithOvalInRect:layer2.bounds].CGPath;
    layer2.fillColor = [UIColor blueColor].CGColor;
    [containerLayer addSublayer:layer2];
}

Output:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!