Let's say that I make an animation like so:
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
someView.frame = CGRect(0, 100, 200, 200);
}
completion:nil];
And Let's say that I have subviews inside someView that have their layoutSubviews method overridden. I would like to identify an ongoing animation inside that layoutSubviews and make corresponding actions depending if the animation I detected was created by me, or if it is for example an animation caused by device rotation.
To get the animations in progress I use the following code:
for (NSString *animationKey in [self.layer animationKeys])
{
CAAnimation *animation = [self.layer animationForKey:animationKey];
// ... Do something ...
}
Now If I would be using CABasicAnimation I could set arbitrary properties using:
[myAnimation setValue:@"mySuperCoolAnimation" forKey:@"AnimationName"];
And then check that key on the other side. But I would like to avoid using CABasicAnimation because of my complex view hierarchy.
So for simplicity sake is there a way to do something like that using UIView animateWithDuration:
Could you just raise a flag to say your animation is in progress, and lower it in the completion block?
Or use the current CATransaction to store an arbitrary key, as you would have done with the CABasicAnimation? It is a little-known fact that there is always a CATransaction, which automatically commits and runs the animations at the end of the run loop (after your own code finishes), and then optionally fires a completion handler when it finishes - that, in fact, is how animation works, as I explain in my book:
http://www.apeth.com/iOSBook/ch17.html#_animation_transactions
(See esp. "The Truth About Transactions", further down that page.)
This is cool for various reasons:
The current CATransaction is available from your code, even when your code has nothing to do with animation at that moment.
You can make your own transaction, wrapping your animations, and commit it (this might be the solution you're after; I can't quite tell)
CATransaction has a completion handler. I often use this trick to do something that must follow on right after one of my animations.
CATransaction fires its completion handler after its animations finish, even if they are not your animations; I use this trick as a specialized form of delayed performance, to wait until one of the runtime's own internally generated animations ends.
CATransaction can store an arbitrary key/value pair, just like CALayer and CAAnimation. I often use this as a way of passing a signal through the transaction, to be picked up when the completion handler runs.
Sorry not to propose any specific solution here, but I'm just suggesting that knowing about CATransaction might allow you to work out something appropriate to your case.