我目前正在处理一个困扰我很多的问题。我正在开发一个无休止的可滚动年份视图,它非常类似于标准的苹果日历应用程序:
所以我基本上有2个数组,第一个包含每月第一天的相应工作日,第二个包含我想要显示的月份的总天数。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
int currentYearIndex = (int)indexPath.section * 12;
int fillDays = [[self.dayIndexThisMonthArray objectAtIndex:indexPath.row + currentYearIndex ]intValue];
int daysThisMonth = [[self.daysThisMonthArray objectAtIndex:indexPath.row + currentYearIndex ]intValue];
EMYearCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YearCell" forIndexPath:indexPath];
[cell configureForNumberOfFillDays: fillDays daysThisMonth: daysThisMonth];
return cell;
}
我创建了一个自定义的UICollectionView类,它接收了aford-said的2个信息并在创建相应月份后小心:
#import "EMYearCell.h"
@interface EMYearCell()
@end
@implementation EMYearCell
- (void)configureForNumberOfFillDays: (int)fillDays daysThisMonth: (int)daysThisMonth
{
@autoreleasepool {
void (^labelingBlock) (int, int, int)= ^(int horizontalSpacingMultiplier, int i, int fillDayAdjuster){
self.theLabel = [[EMCalendarLabel alloc]initWithFrame:CGRectMake(self.initialXCord + i * self.distanceBetweenLabels, self.initialYCord + self.horizontalSpacing * horizontalSpacingMultiplier, 50, 50)];
[self.theLabel setText:[NSString stringWithFormat:@"%d",i + fillDayAdjuster - fillDays]];
[self addSubview:self.theLabel];
};
for (int i = fillDays; i < 7; i++){
labelingBlock (1,i,1);
}
for (int i = 0; i < 7; i++){
labelingBlock (2,i,8);
}
for (int i = 0; i < 7; i++){
labelingBlock (3,i,15);
}
for (int i = 0; i < 7; i++){
labelingBlock (4,i,22);
}
for (int i = 0; i < 7; i++){
if (i + 29 - fillDays < daysThisMonth + 1){
labelingBlock (5,i,29);
}
}
for (int i = 0; i < 7; i++){
if (i + 36 - fillDays < daysThisMonth + 1){
labelingBlock (6,i,36);
}
}
}
}
尽管事实上我只是将一堆标签作为子视图添加到我的自定义UICollectionView单元格中,但滚动速度非常缓慢而且生涩。我真的想不出任何可能使这一切变得更便宜。
我尝试了这个:
[self.view.layer setShouldRasterize:YES];
[self.view.layer setRasterizationScale:[UIScreen mainScreen].scale];
但我也没有发现任何改善。
我非常感谢任何可以帮助我解决这种情况的建议。
答案 0 :(得分:2)
您可以简单地执行以下操作:
@interface EMYearCell : UICollectionViewCell
@property (nonatomic, strong) NSOperation *updateOperation;
@end
@interface MyCollectionViewController ()
@property NSOperationQueue *operationQueue;
@end
- (void)viewDidLoad
{
[super viewDidLoad];
self.operationQueue = [NSOperationQueue new];
self.operationQueue.maxConcurrentOperationCount = 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
EMYearCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: @"YearCell" forIndexPath: indexPath];
// Increment the cell's tag
NSInteger currentTag = cell.tag + 1;
cell.tag = currentTag;
NSBlockOperation *updateOperation = [NSBlockOperation blockOperationWithBlock:^{
// preparing data for the cell here
dispatch_sync(dispatch_get_main_queue(), ^{
// Only update the cell if the cell tag hasn't changed. Otherwise, the cell has been re-used.
if (cell.tag == currentTag) {
// update the cell here
}
});
}];
cell.updateOperation = updateOperation;
[self.operationQueue addOperation:updateOperation];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
// cancel the associated update operation
EMYearCell *yearCell = (EMYearCell *)cell;
[yearCell.updateOperation cancel];
}
答案 1 :(得分:1)
我通过添加UICollectionViewCell
作为子图层替换了我的自定义CATextLayer
中的所有标签,etvoilà,突然滚动是如此柔滑。