我正在尝试制作多个 uiview 堆栈。每个 UIView 都有阴影、轻微的旋转和缩放。作为测试,我制作了 10 个堆栈的 10 个视图。绘制所有这些非常慢..有没有优化这个的好方法?我尝试从图像中制作阴影和背景,但这很丑而且同样慢。我把这些堆栈放在 UIScrollView
for (int k = 0; k < 9; k++) {
for (int i = 0; i < 9; i++) {
UIView *stack = [[UIView alloc] initWithFrame:CGRectMake((i * 106), (k * 106), 110, 110)];
for (int i = 0; i < 10; i++) {
CardView *cardView = [[CardView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
//cardView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"card_background.png"]];
cardView.layer.shadowOffset = CGSizeMake(0, 0);
cardView.layer.shadowOpacity = 0.3f;
cardView.layer.shadowPath = [UIBezierPath bezierPathWithRect:(CGRect){CGPointZero, cardView.layer.bounds.size}].CGPath; // Set shadow path, without this the performance is *really* bad
cardView.transform = CGAffineTransformMakeRotation(((arc4random() % 20) - 10.0f) / 100.0f);
cardView.transform = CGAffineTransformScale(cardView.transform, 0.35, 0.35);
cardView.layer.shouldRasterize = YES;
cardView.layer.rasterizationScale = 0.5;
cardView.center = CGPointMake(55, 55);
[stack addSubview:cardView];
}
[_backgroundView addSubview:stack];
}
}
编辑1;尝试了一些东西,禁用光栅化没有多大帮助,禁用阴影也没有多大帮助,旋转和缩放对于这么多的uiview来说也是资源密集型的。异步绘图(当时一个堆栈)是一种选择吗?
编辑2;无论如何,猜猜制作 100 个 UIView 的速度很慢。如果我找到更好的(我猜是异步或类似的)解决方案,我会报告