OC-设置阴影,怎么避免离屏渲染?

一般我们设置阴影就是以下几句代码就可以简单设置了, 不过这样一般会造成离屏渲染

OC-设置阴影,怎么避免离屏渲染?

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    if (self.bgView.layer.cornerRadius != 4) {
        self.bgView.layer.cornerRadius = 4;
        self.bgView.layer.shadowColor = [UIColor blackColor].CGColor;//shadowColor阴影颜色
        self.bgView.layer.shadowOffset = CGSizeMake(0,0);//shadowOffset阴影偏移,x向右偏移2,y向下偏移6,默认(0, -3),这个跟shadowRadius配合使用
        self.bgView.layer.shadowOpacity = 0.3;//阴影透明度,默认0
        self.bgView.layer.shadowRadius = 4;//阴影半径,默认3
    }
}

实则多一句代码就可以避免离屏渲染(是不是感觉老尴尬了啊)

self.bgView.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:self.bgView.bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight|UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(self.bgView.layer.cornerRadius, self.bgView.layer.cornerRadius)].CGPath;//参数依次为大小,设置四个角圆角状态,圆角曲度  设置阴影路径可避免离屏渲染

最终代码

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    if (self.bgView.layer.cornerRadius != 4) {
        self.bgView.layer.cornerRadius = 4;
        self.bgView.layer.shadowColor = [UIColor blackColor].CGColor;//shadowColor阴影颜色
        self.bgView.layer.shadowOffset = CGSizeMake(0,0);//shadowOffset阴影偏移,x向右偏移2,y向下偏移6,默认(0, -3),这个跟shadowRadius配合使用
        self.bgView.layer.shadowOpacity = 0.3;//阴影透明度,默认0
        self.bgView.layer.shadowRadius = 4;//阴影半径,默认3
        self.bgView.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:self.bgView.bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight|UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(self.bgView.layer.cornerRadius, self.bgView.layer.cornerRadius)].CGPath;//参数依次为大小,设置四个角圆角状态,圆角曲度  设置阴影路径可避免离屏渲染
    }
}

OC-设置阴影,怎么避免离屏渲染?

查看离屏渲染步骤图

OC-设置阴影,怎么避免离屏渲染?

© 版权声明
THE END
如果内容对您有所帮助,就支持一下吧!
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容