UICollectionView滚动不顺畅。 为什么?(UICollectionView scr

2019-10-18 07:53发布

为什么我UICollecionView的滚动不流畅?

这是代码:

static NSString *CellIdentifier = @"cellRecipe";

// Configure the cell...

[cell setBackgroundColor: [UIColor colorWithPatternImage:[UIImage imageNamed:@"bgRecipeCell.png"]]];

cell.image.layer.masksToBounds = YES;
cell.image.layer.cornerRadius = 8.0f;
cell.image.layer.borderColor = [UIColor blackColor].CGColor;
cell.image.layer.borderWidth = 1.0f;

Recipes *recipe = [recipesArray_ objectAtIndex:indexPath.item];

//checking for purchase
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSObject *object = [prefs objectForKey:@"com.__.__.recipefree"];

if ([recipe.isPaid integerValue] == 0) {
    [cell.freeImage setHidden:NO];
}
else [cell.freeImage setHidden:YES];

if ([object isEqual:[NSNumber numberWithInt:1]]) {
    [cell.freeImage setHidden:YES];
}

if ([recipe.isFavorite integerValue] == 0) {
    [cell.favImage setImage:[UIImage imageNamed:@"toFavorite.png"] ];
}
else [cell.favImage setImage: [UIImage imageNamed:@"toFavorite_.png"]];

[cell.recipeName setText: recipe.name];
[cell.image setImage:recipe.thumbImage];

MainTabController *mainTabController = [[MainTabController alloc] init];
int difficulty = [recipe.difficulty intValue];
//this is the function which return an image for difficulty level from 1 to 5
[cell.difficultyImage setImage: [mainTabController imageForRating:difficulty]];

return cell;

Answer 1:

在我的情况下,该层的边界被打破了平滑度。 正如我真想边界在那里,我不能使用图片(背景颜色必须是可变的),我用下面的UIView层次解决它:

UIView with rounded corners coloured with "border colour"
    UIView with rounded corners coloured with "content colour"


Answer 2:

这是一个老问题,但在情况下,如果有人还在寻找答案再短的诀窍是,在cellForItemAtIndexPath ,首先dequeuecollectionView细胞:

CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

然后添加下面一行:

cell.layer.shouldRasterize = YES; cell.layer.rasterizationScale = [UIScreen mainScreen].scale;



文章来源: UICollectionView scrolling is not smooth. Why?