6

I'm currently using the following animation on a UITableViewCell:

CATransform3D rotationTransform = CATransform3DMakeRotation(1.0f * M_PI, 0, 0, 1.0);
CABasicAnimation* rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];

rotationAnimation.toValue = [NSValue valueWithCATransform3D:rotationTransform];
rotationAnimation.duration = 0.25f;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 1;

[cell.rotatingImage.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

However, when ~3 cells are animated as above the animations become very laggy. Is there any way I can reduce this lag?

4

1 回答 1

1

我要做的第一件事是将动画创建代码从-tableView:cellForRowAtIndexPath:方法中删除到(比如说)viewDidLoad. 然后将动画添加到-tableView:cellForRowAtIndexPath:方法中的单元格中。

对象创建和矩阵计算代价高昂,因此每次调用-tableView:cellForRowAtIndexPath:都会减慢代码速度。

在代码中,我会有类似于以下内容的内容:

- (void) viewDidLoad 
{
    // Normal viewDidLoad code above
    ...

    // Assume that rotationAnimation is an instance variable of type CABasicAnimation*;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];

    CATransform3D rotationTransform = CATransform3DMakeRotation(1.0f * M_PI, 0, 0, 1.0);

    rotationAnimation.toValue = [NSValue valueWithCATransform3D:rotationTransform];
    rotationAnimation.duration = 0.25f;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // create cell
    ...
    // Now apply the animation to the necessary layer.
    [cell.rotatingImage.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

    return cell;
}

这行吗?

于 2012-05-17T22:08:47.043 回答