Search code examples
objective-ciosipadcore-animation

Core Animation animation lag


I am trying to fade in a few views in using Core Animation. Everything is alright until the 4th or 5th view is faded in. The animation stops and a few seconds later the rest of the views is just put on screen (no animation is happening). I tested it on an 3rd generation iPad, so it can't be an issue of outdated hardware. Code:

viewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];
    self.subViews = self.view.subviews;

    for (UIView *view in self.subViews) {
        view.backgroundColor = [UIColor clearColor];
        PhotoFrame *photoFrame = [[PhotoFrame alloc] init];
        photoFrame.photo.image = [UIImage imageNamed:[NSString stringWithFormat:@"test%d.jpg", arc4random() % 6 + 1]];
        [view addSubview:photoFrame];
        view.layer.opacity = 0;
    }
}

viewDidAppear

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self startAnimations];
}

startAnimations

- (void)startAnimations {
    int count = 1;
    for (UIView *view in self.subViews) {
        CALayer *animationLayer = view.layer;

        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
        animation.fromValue = @0.0;
        animation.toValue = @1.0;
        animation.duration = 0.3;
        animation.beginTime = CACurrentMediaTime() + count * 0.3;
        animation.removedOnCompletion = NO;
        animation.fillMode = kCAFillModeForwards;

        [animationLayer addAnimation:animation forKey:@"opacityAnimation"];
        count++;
    }
}

I hope that anyone can help me.


Solution

  • I would suggest either switching to UIView animation methods like animateWithDuration:animations:, or setting your properties to their end values as you submit your animations and NOT using removedOnCompletion = TRUE. As I said in a comment above, removedOnCompletion actually causes the animations to be kept active, and applied to each frame of rendering. I've found that when there are a lot of animations active, things slow down.