简体   繁体   中英

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.

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.

Why don't you use UIView animateWithDuration?

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIView_Class/UIView/UIView.html

It will fade in your views just as well and I've never had any problems with it.

You could try doing all the animations within a CATransaction to batch them together or you could add them all to a common container view and only animate the opacity of the container view. Since the views shouldn't need to redraw during the animation you could also use shouldRasterize = YES;

All of these things may or may not affect performance, rasterizertion could even make it worse in some cases.

Also, if there is shadows involved you should really set a shadow path for every shadow as this will greatly improve performance when shadows are involved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM