美文网首页
iOS切圆角方法解决离屏渲染问题

iOS切圆角方法解决离屏渲染问题

作者: ZouJgg | 来源:发表于2021-12-31 15:42 被阅读0次

1、UIView切圆角
UIView在切圆角的时候使用

view.layer.cornerRadius = 8;
view.layer.masksToBounds = YES;

会导致离屏渲染。

解决办法:不能使用masksToBounds,view.clipsToBounds;直接切圆角:

view.layer.cornerRadius = 8;

这是方法适用于切全圆角,如果是切指定圆角的时候就不行了;
增加切指定圆角的方法

/**
 * setCornerRadius   给view设置圆角
 * @param value      圆角大小
 * @param rectCorner 圆角位置
 **/
- (void)setCornerRadius:(CGFloat)radius byRoundingCorners:(UIRectCorner)corners {
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = self.bounds;
    shapeLayer.path = path.CGPath;
    self.layer.mask = shapeLayer;
}

使用改方法满足了切指定圆角的需求;但是又会导致离屏渲染问题;
这儿想到一个办法就是使用UIImageView替换UIView;或者是在底层插入UIImageView;
首先要设置一个UIImage根据背景设置

[self.imgBGView setImage:[UIImage imageWithColor:[UIColor whiteColor]]];

方法实现

+ (UIImage *)imageWithColor:(UIColor *)color {
    return [self imageWithColor:color size:CGSizeMake(1, 1)];
}

+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
    if (!color || size.width <= 0 || size.height <= 0) return nil;
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

之后再,具体实现见下面UIImageView切圆角方法;

2、UIView增加阴影效果:

view.layer.shadowColor = [UIColor redColor].CGColor;
view.layer.shadowOpacity = 0.5;
view.layer.shadowRadius = 8.0;
view.layer.shadowOffset = CGSizeMake(0, 3);

会导致离屏渲染。
解决办法:设置阴影路径可避免离屏渲染

view.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(8, 8)].CGPath;

实现上述方法就可以实现圆角+阴影效果

3、UIImageView切圆角
简便的方法

imgView.layer.cornerRadius = 8;
imgView.layer.masksToBounds = YES;

在iOS9.0之后加载png图片设置圆角不会导致离屏渲染;
但是如果在给UIImageView增加一个背景色就会导致离屏渲染;

最优的解决办法:通过给UIImageView加载的图片切圆角;
给图片切圆角之后重新赋值给UIImageView;如下

/// 给UIImageView 切指定圆角
- (UIImage *)imageWithCornerRadius:(CGFloat)radius rectCorner:(UIRectCorner)corners {
    CGRect rect = (CGRect){0.f, 0.f, self.size};

    UIGraphicsBeginImageContextWithOptions(self.size, NO, UIScreen.mainScreen.scale);
    CGSize cornerRadii = CGSizeMake(radius, radius);
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:cornerRadii];
    CGContextAddPath(UIGraphicsGetCurrentContext(),bezierPath.CGPath);
    CGContextClip(UIGraphicsGetCurrentContext());
    [self drawRect:self.bounds];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.image = image;
    return image;
}

需要给定size时,如果使用的Masnory添加的预约,则需要立即更新之后才会生效

[self updateConstraintsIfNeeded];
[self layoutIfNeeded];

将持续优化方法,找到最适合的方法

相关文章

网友评论

      本文标题:iOS切圆角方法解决离屏渲染问题

      本文链接:https://www.haomeiwen.com/subject/zkngqrtx.html