2012-05-14 32 views
6

我目前使用在UITableViewCell以下动画制作:CABasicAnimation多么容易

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"]; 

然而,当〜3个单元的动画为动画上面变得非常laggy。有什么办法可以减少这种滞后吗?

+0

我不认为在单元格内设置动画是一个好主意,我发现当表滚动时,使用CADisplayLink的东西会暂停(可能是其他内容)。也许你可以尝试仅使活动单元格或沿着这些行的东西动画。 – EmilioPelaez

+0

在我的情况下,我不知道哪些细胞将与动画..所以hardcode/statis不是最好的变种... – LightNight

+1

你旋转的图像有多大?还有哪些其他属性应用于图层?随着一个小图像,我不知道我的iPhone 4滞后。 –

回答

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; 
} 

这是否做到这一点?

+0

你测试了这个?应用程序崩溃。 – LightNight

+0

是的:你需要保留'rotationAnimation'对象,如下所示: 'rotationAnimation = [[CABasicAnimation animationWithKeyPath:@“transform”] retain];' –

+1

@MihaiFratu感谢您指出这一点。 – haroldcampbell