Kevin Sliech
Kevin Sliech

Reputation: 431

CATransaction Animations Occurring With setDisableActions:YES

I have a number of CALayer/CAShapeLayer/CATextLayer objects that need to be modified simultaneously, but only a subset should animate. For those layers that I do not wish to animate, I wrap the update in CATransaction calls like this:

- (void) setPlayerName:(NSString *)playerName
{
    _playerName = playerName;

    [CATransaction begin];
    [CATransaction setDisableActions:YES];

    [self updateNameLayer]; // Layer property updates happen in here

    [CATransaction commit];

    // Update the screen
    [self setNeedsLayout];
    [self setNeedsDisplay];
}

It wasn't until I used a [CATransaction setDisableActions:YES]; outside of CATransaction begin/commit calls that I realized animations could be affected on separate objects that were set to animate at the same time. In that case, all of my animations for that update were disabled.

The solution, I thought, was to explicitly wrap each update in CATransaction begin/commit calls to localize the setDisableActions: call to that transaction. Even after doing this, it still seems that the animations are either all or nothing. Sounds to me like I don't have a good enough understanding of how CATransaction works.

What is the correct way to handle several, simultaneous CALayer implicitly-animated property updates but only animate a subset of them? These update calls and layers are scattered across multiple objects.

Upvotes: 3

Views: 3386

Answers (1)

matt
matt

Reputation: 534893

Certainly what you're trying to do can work. For example:

    [CATransaction begin];
    [CATransaction setDisableActions:YES];
    self.lay1.position = CGPointMake(300,300);
    [CATransaction commit];

    self.lay2.position = CGPointMake(300,300);

Those two layers move to the same place, but one of them is animated and the other isn't.

The fact that your code isn't working the way you want can't be explained without further information. (For example, you may be giving a layer contradictory commands, thus causing the animation to be canceled.) But you are definitely on a legitimate track. If you find this too daunting, however, I recommend just abandoning implicit layer animation and instead using CABasicAnimation and friends.

Upvotes: 4

Related Questions