在单元格上设置圆angular半径会导致UICollectionView的性能下降

我有一个只有几个单元格(约20)的UICollectionView 。 这个集合的性能很好。 但是,当我尝试围绕由这个视图呈现的UICollectionViewCells的angular落时,我的performance受到重创。 在我的单元格的init方法中,这是我添加的唯一原因:

 [self.layer setCornerRadius:15]; 

由于这是在init方法中,我正在重用这些单元格,所以我不明白为什么这会引起我的问​​题。

我已经尝试使用以下的多个组合来调整销售的光栅化和不透明度,但仍然没有效果:

 [self.layer setMasksToBounds:YES]; [self.layer setCornerRadius:15]; [self.layer setRasterizationScale:[[UIScreen mainScreen] scale]]; self.layer.shouldRasterize = YES; self.layer.opaque = YES; 

是他们的一些设置或技巧来提高具有圆angular单元格的UICollectionView的性能?

正如@Till在评论中指出的那样,预先渲染的图像可以解决您的性能问题。 你可以把所有的angular落四舍五入,阴影,和其他任何特殊效果,而不是需要CA来dynamic渲染它们。

预渲染的图像不会将您locking到静态内容大小,要么:查看UIImage可resize的图像的东西。 (这比CA渲染每一帧还要快。)

我发现,这完全是因为要求出队的队列。 每次调用时,都需要重新渲染带有圆angular的单元格。 如果collections视图在屏幕上滚动时没有将其从视图中移除,则性能不会受到影响(只要它们的集合中的项目不是太多)。 看起来像一把双刃剑 – 两种方式都有其局限性。

有一个UIView子类的代码,它提供了一个不透明的圆形边界和中间的透明洞的视图。 您应该像往常一样创build所需的视图,之后您可以在视图上添加视图。 可视化在这里 。

它适用于如果您使用UICollectionView或UITableView的单色背景,您可以为每个单元添加以下子视图:

 @interface TPRoundedFrameView : UIView @property (assign, nonatomic) CGFloat cornerRadius; @property (strong, nonatomic) UIColor * borderColor; @end @implementation TPRoundedFrameView - (instancetype)init { if ((self = [super init])) { self.opaque = NO; self.backgroundColor = [UIColor clearColor]; } return self; } - (void)drawRect:(CGRect)rect { [super drawRect:rect]; UIBezierPath * path = [UIBezierPath bezierPathWithRect:rect]; UIBezierPath * innerPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:self.cornerRadius]; [path appendPath:[innerPath bezierPathByReversingPath]]; [self.borderColor set]; [path fill]; } @end 

目标单元类示例:

 - (void)awakeFromNib { [super awakeFromNib]; // creating holed view with rounded corners self.myRoundedView.backgroundColor = [UIColor whiteColor]; TPRoundedFrameView * roundedFrame = [TPRoundedFrameView new]; roundedFrame.cornerRadius = 5.f; roundedFrame.borderColor = [UIColor groupTableViewBackgroundColor]; // add borders to your view with appropriate constraints [self.myRoundedView addSubview:roundedFrame]; roundedFrame.translatesAutoresizingMaskIntoConstraints = NO; NSDictionary * views = NSDictionaryOfVariableBindings(roundedFrame); NSArray * horizontal = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[roundedFrame]-0-|" options:0 metrics:nil views:views]; NSArray * vertical = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[roundedFrame]-0-|" options:0 metrics:nil views:views]; [self.myRoundedView addConstraints:horizontal]; [self.myRoundedView addConstraints:vertical]; } 

结果:具有圆angular视图的表格为单元格

我通过在contentView上应用此半径而不是单元本身来修复了我所有的性能borderRadius问题。

 self.contentView.layer.borderWidth = 1.0f; self.contentView.layer.cornerRadius = 5.0f; self.contentView.layer.borderColor = [UIColor colorWithRed:202/255. green:202/255. blue:202/255. alpha:1.0].CGColor;