[英]Table view with UIBlurEffect become laggy
我在使用uiblurview實現頂欄后在iphone5上滯后於tableview,在添加模糊視圖之前,tableview占用20-25%的cpu。 工作完美。 期待任何性能幫助
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"Cell%li%i",(long)indexPath.row,cellvar]];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"Cell%li%i",(long)indexPath.row,cellvar]];
if ([self.obrazki count]>0) {
CGRect imageFrame = CGRectMake((cell.frame.size.width/tableViewOffset)-(tableView.frame.size.width*0.92/2), 10, tableView.frame.size.width*0.92, tableView.frame.size.width*0.92);
UIImageView * image = [[UIImageView alloc] initWithFrame:imageFrame];
image.layer.masksToBounds = YES;
image.layer.cornerRadius = 25;
[image setImage:[self.obrazki objectAtIndex:indexPath.row]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
image.backgroundColor = [UIColor clearColor];
[cell addSubview:image];
cell.backgroundColor = [UIColor clearColor];
UIVisualEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *visualEffectView= [[UIVisualEffectView alloc] initWithEffect:blurEffect];
visualEffectView.frame = CGRectMake((cell.frame.size.width/tableViewOffset)-(tableView.frame.size.width*0.92/2)-0.5, 9,tableView.frame.size.width*0.92+1,35);
CAShapeLayer * maskLayer = [CAShapeLayer layer];
maskLayer.path = [UIBezierPath bezierPathWithRoundedRect: visualEffectView.bounds byRoundingCorners: UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii: (CGSize){25.0, 25.0}].CGPath;
visualEffectView.layer.mask = maskLayer;
visualEffectView.layer.masksToBounds = YES;
[cell addSubview:visualEffectView];
UILabel *likeCount = [[UILabel alloc] initWithFrame:CGRectMake((cell.frame.size.width/tableViewOffset)-(tableView.frame.size.width*0.92/2)+distanceFromStart, 4,tableView.frame.size.width*0.92,35)];
likeCount.text = [NSString stringWithFormat:@"%@",[imagesLikes objectAtIndex:indexPath.row]];
likeCount.backgroundColor = [UIColor clearColor];
likeCount.tintColor = [UIColor blackColor];
likeCount.font = [UIFont fontWithName:@"AppleSDGothicNeo-Thin" size:20];
[visualEffectView addSubview:likeCount];
CGRect imageLikeFrame = CGRectMake(likeCount.frame.origin.x-20, likeCount.frame.origin.y+7.5, 16, 16);
UIImageView * imageLike = [[UIImageView alloc] initWithFrame:imageLikeFrame];
[imageLike setImage:[UIImage imageNamed:@"like.png"]];
imageLike.layer.masksToBounds=YES;
imageLike.backgroundColor = [UIColor clearColor];
[visualEffectView addSubview:imageLike];
NSLog(@"Generating:%i",indexPath.row);
}
}
// NSLog(@"LoadingOnView:%i",indexPath.row);
return cell;
}
您正在為每一行創建一個單元格(UITableViewCell的實例),而不是重用出隊的單元格。 出於性能原因,這里有“ dequeueReusableCell ...”。 使用它出隊並重用其他行的單元格。
對所有單元格使用恆定的單元格標識符,只需為每一行修改單元格。 即,更改圖像或文本。 但是,如果您想提高性能,請重用這些單元。 那是第一件事。 然后,告訴我們性能是否足夠,或者是否需要更多優化。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.