Question

So, I've got a CALayer from a UITableView instance. I first want to set a couple properties like the backgroundColor. Afterwards I add an animation that is supposed to make the reloading (-reloadData) look nicer. However, the CATransition I'm adding also animates the backgroundColor I set before. I guess I'm missing something real basic here but I really don't get what.

Here's my code:

    self.superview.backgroundColor = [UIColor greenColor];
    self.backgroundColor = nil;
    self.backgroundView.backgroundColor = nil;

I want the UITableView to be green immediately.

Here's the transition:

CATransition* swapAnimation = [CATransition animation];
            swapAnimation.type = kCATransitionPush;
            swapAnimation.subtype = kCATransitionTypeFromUITableViewRowAnimation(animation);
            swapAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
            swapAnimation.fillMode = kCAFillModeBoth;
            swapAnimation.duration = 5.0f;
            swapAnimation.removedOnCompletion = YES;
            [self.layer addAnimation:swapAnimation forKey:@"UITableViewReloadDataAnimationKey"];

When I'm running this, the UITableView keeps its original backgroundColor, the green (which is actually shining through the now transparent UITableView) slides in from the top/bottom with the cells. It's code from a category explaining the self.layer calls.

Was it helpful?

Solution

Looks like you need to wrap the background color setter in an explicit CATransaction, because it's not getting flushed until after you add the animation.

Like so:

[CATransaction begin];
// set background
[CATransaction flush];
[CATransaction commit];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top