CABasicAnimation如何让它变得简单

Lig*_*ght 6 iphone core-animation objective-c cabasicanimation ios

我目前正在使用以下动画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"];
Run Code Online (Sandbox Code Playgroud)

然而,当如上所述动画~3个单元时,动画变得非常迟钝.有什么办法可以减少这种滞后吗?

har*_*ell 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;
}
Run Code Online (Sandbox Code Playgroud)

这个有作用吗?