最近发现项目中自己写的列表在滑动时有些卡,而同事的列表很顺畅,两个列表显示的内容基本相同,都是几个imageView和几个label还有1个按钮,功能上主要是异步加载图片.
经过分析查找,最终发现问题的根源在于图标的圆角处理方式:
我的列表的cell在处理圆角时直接使用 :
iconImageView = [[UIImageView alloc]init];
iconImageView.layer.cornerRadius = 12;
而同事处理圆角时使用CAShapeLayer处理:
@interface CollectionViewCellImageView : UIImageView
@property (nonatomic, strong) CAShapeLayer * maskLayer;
@property (nonatomic, assign) CGFloat maskCornerRadius;
@property (nonatomic, strong) NSURL * url;
@end
@implementation CollectionViewCellImageView
- (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 = hllColor(120, 120, 120, 0.3).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:self.maskCornerRadius]];
_maskLayer.path = path.CGPath;
}
@end
创建UIImageView的子类,使用时
iconImageView = [[CollectionViewCellImageView alloc]init];
iconImageView.maskCornerRadius = 12.0f;