tableViewCell多个按钮切圆角滑动卡的原因及解决

最近在写项目,tableViewCell上有多个选集的按钮,每个按钮都设置了圆角,导致在滑动动时卡顿,于是上网找资料,经过分析查找,最终发现问题的根源在于圆角处理方式:

我的列表的cell在处理圆角时直接使用 :


iconImageView = [[UIImageView alloc]init];

iconImageView.layer.cornerRadius = 12;

解决方法是处理圆角时使用CAShapeLayer处理:

@interface numCellButton : UIButton

@property (nonatomic, strong) CAShapeLayer *maskLayer;
@property (nonatomic, assign) CGFloat   maskCornerRadius;

@end

- (instancetype)init {
    self = [super init];
    if (self) {
        _maskLayer = [CAShapeLayer new];
        _maskLayer.fillColor = [UIColor whiteColor].CGColor;
        _maskLayer.fillRule = kCAFillRuleEvenOdd;
        [self.layer addSublayer:_maskLayer];
    }
    return self;
}

- (void)setMaskCornerRadius:(CGFloat)maskCornerRadius {
    _maskCornerRadius = maskCornerRadius;
    _maskLayer.borderWidth = 0.5;
    _maskLayer.borderColor = [UIColor grayColor].CGColor;
    _maskLayer.cornerRadius = maskCornerRadius;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _maskLayer.frame = self.layer.bounds;
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.layer.bounds];
    [path appendPath:[UIBezierPath bezierPathWithRoundedRect:self.layer.bounds cornerRadius:_maskCornerRadius]];
    _maskLayer.path = path.CGPath;
}


@end

创建UIImageView的子类,使用时

numCellButton *btn = [[numCellButton alloc] init];
btn.maskCornerRadius = 3.0f;

修改后,列表滑动非常流畅

此文章的解决方法来源于C博客,请支持原作者。
原文地址:http://blog.csdn.net/borntofight/article/details/37883441

你可能感兴趣的:(tableViewCell多个按钮切圆角滑动卡的原因及解决)