考虑到性能,如何在UICollectionViewCell内旋转UILabel?

时间:2013-01-11 09:07:24

标签: ios performance reusability uicollectionview uicollectionviewcell

我有一个UICollectionView我正在重复使用许多UICollectionViewCells产品信息,想想Pinterest。

但是我有一个价格标签,并希望为每个单元格旋转45度的价格带。

please note the green price ribbon

实现这一目标的最佳性能方法是什么?

我尝试过:

#import <QuartzCore/QuartzCore.h>

@interface ItemCollectionViewCell : UICollectionViewCell

@property (weak, nonatomic) IBOutlet UILabel *price;

@end


@implementation ItemCollectionViewCell

@synthesize price;

- (void)awakeFromNib {
    price.layer.transform = CATransform3DMakeRotation(M_PI_4, 0, 0, 1);
    price.layer.transform = CATransform3DTranslate(price.transform, 25, -15, 0);
    price.layer.shouldRasterize = YES;
}

@end

但是滚动并将新视图推入导航控制器的整体结果非常慢。

更新

  • 似乎iOS6 Autolayout是性能下降的主要原因,但我仍然不知道如何修复此问题或仅为价格标签删除自动布局。

1 个答案:

答案 0 :(得分:3)

对我来说有用的是删除UICollectionViewCell内的iOS6 autoLayout功能所定义的属性,它为price标签设置了约束,为:

- (void)awakeFromNib {

    NSMutableArray* cons = [NSMutableArray array];
    //iterating through UICollectionViewCell constraint list
    for (NSLayoutConstraint* con in self.constraints) {
        //if the target constraint is `price` then add that to the array
        if (con.firstItem == price || con.secondItem == price) {
            [cons addObject:con];
        }
    }
    //remove the unwanted constraints array to the UICollectionViewCell
    [self removeConstraints:cons];

    //ready to roll…
    price.layer.transform = CATransform3DMakeRotation(M_PI_4, 0, 0, 1);
    price.layer.transform = CATransform3DTranslate(price.transform, 25, -15, 0);
    price.layer.shouldRasterize = YES;
}