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?

kevboh
  • 5,207
  • 5
  • 38
  • 54
LightNight
  • 1,616
  • 6
  • 30
  • 59
  • I don't think animating something inside a cell is a good idea, I've found that when tables are scrolling, things that use the CADisplayLink are paused (could be something else). Maybe you could try to animate ONLY the active cell or something along those lines. – EmilioPelaez May 14 '12 at 22:20
  • In my case I don't know which cells will be with animation.. so hardcode/statis is not the best variant... – LightNight May 15 '12 at 00:19
  • 1
    How large is the image you're rotating? What other properties are applied to the layer? With a small image, I'm not noticing a lag on my iPhone 4. – Brandon Schlenker May 17 '12 at 19:29

1 Answers1

1

The first things that I would is remove the animation creation code from the -tableView:cellForRowAtIndexPath: method to (say) the viewDidLoad. Then add the animation to the cell in the -tableView:cellForRowAtIndexPath: method.

Object creation and matrix calculations are expensive, so doing them for each call to the -tableView:cellForRowAtIndexPath: is going to slow down your code.

In code, I'd have something similar to the following:

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

Does this do it?

haroldcampbell
  • 1,512
  • 1
  • 14
  • 22