【问题标题】:How to rotate a UILabel inside a UICollectionViewCell considering performance?考虑到性能,如何在 UICollectionViewCell 内旋转 UILabel?
【发布时间】:2012-12-25 20:03:26
【问题描述】:

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

但我有一个价格标签,我想为每个单元格将价格带旋转大约 45 度。

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

我试过了:

#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 自动布局是性能下降的主要原因,但我仍然不知道如何解决这个问题或仅针对价格标签删除自动布局。

【问题讨论】:

  • 您是否对应用程序进行了时间分析,以查看时间花费在哪里。我怀疑不是旋转标签。
  • 我还没有分析它,但是一旦我从代码中删除转换方法,一切都会变得非常快。
  • Fogmeister:我刚刚发现,当在主 Storyboard 上停用 iOS6 自动布局时,一切都变得很糟糕。它肯定很难同时旋转并保持其疯狂的逻辑。知道如何解决或停用仅针对价格标签的 AutoLayout 吗?欢呼

标签: ios performance reusability uicollectionview uicollectionviewcell


【解决方案1】:

对我有用的是删除 UICollectionViewCell 内的 iOS6 自动布局功能定义的属性,它为 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-09
    • 2023-03-18
    • 2012-07-25
    • 1970-01-01
    • 2019-07-11
    相关资源
    最近更新 更多