is there a way to run 2 NSAnimation objects simultaneously?
cocoa, core-image, nsanimation, objective-c, osx-lion
Solution
`NSAnimation` is not really the best choice for animating multiple objects and their properties simultaneously.
Instead, you should make your views conform to the `NSAnimatablePropertyContainer` protocol.
You can then set up multiple custom properties as animatable (in addition to the properties already supported by `NSView`), and then you can simply use your views' `animator` proxy to animate the properties:
yourObject.animator.propertyName = finalPropertyValue;
Apart from making animation very simple, it also allows you to animate multiple objects simultaneously using an `NSAnimationContext`:
[NSAnimationContext beginGrouping];
firstObject.animator.propertyName = finalPropertyValue1;
secondObject.animator.propertyName = finalPropertyValue2;
[NSAnimationContext endGrouping];
You can also set the duration and supply a completion handler block:
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0.5];
[[NSAnimationContext currentContext] setCompletionHandler:^{
NSLog(@"animation finished");
}];
firstObject.animator.propertyName = finalPropertyValue1;
secondObject.animator.propertyName = finalPropertyValue2;
[NSAnimationContext endGrouping];
The `NSAnimation` and `NSViewAnimation` classes are much older than the animator proxy support and I highly recommend that you move away from them if possible. Supporting the `NSAnimatablePropertyContainer` protocol is much simpler than managing all the `NSAnimation` delegate stuff. The Lion support for custom timing functions and completion handlers means there's really no need to do that any more.
For a standard `NSView` object, if you want to add animation support to a property in your view, you just need to override the `+defaultAnimationForKey:` method in your view and return an animation for the property:
//declare the default animations for any keys we want to animate
+ (id)defaultAnimationForKey:(NSString *)key
{
//in this case, we want to add animation for our x and y keys
if ([key isEqualToString:@"x"] || [key isEqualToString:@"y"]) {
return [CABasicAnimation animation];
} else {
// Defer to super's implementation for any keys we don't specifically handle.
return [super defaultAnimationForKey:key];
}
}
I've created a simple sample project that shows how to animate multiple properties of a view simultaneously using the `NSAnimatablePropertyContainer` protocol.
All your view needs to do to update successfully is make sure that `setNeedsDisplay:YES` is called when any of the animatable properties are modified. You can then get the values of those properties in your `drawRect:` method and update the animation based on those values.
If you want a simple progress value that is analogous to the way things work with `NSAnimation`, you could define a `progress` property on your view and then do something like this:
yourView.progress = 0;
[yourView.animator setProgress:1.0];
You can then access `self.progress` in your `drawRect:` method to find out the current value of the animation.
Problem
I created 2 `NSAnimation` objects of flipping the view with another view. I'd like to run 2 these animations simultaneously. I cannot use `NSViewAnimation`, since it's now about animating any of view properties. Here is the animation creation: ``` self.animation = [[[TransitionAnimation alloc] initWithDuration:1.0 animationCurve:NSAnimationEaseInOut] autorelease]; [self.animation setDelegate:delegate]; [self.animation setCurrentProgress:0.0]; [self.animation startAnimation]; ``` I tried to link 2 animations, but probably it didn't work for some reason. I took an example from: Apple developer site configuring the `NSAnimation` object to use `NSAnimationNonblocking` doesn't show any animation at all... EDIT: The second animation is exactly the same as the first one and created in the same place the first is created. `TransitionAnimation` is a subclass of `NSAnimation`, where the `setCurrentProgress` looks like that: ``` - (void)setCurrentProgress:(NSAnimationProgress)progress { [super setCurrentProgress:progress]; [(NSView *)[self delegate] display]; } ``` the `delegate` is `NSView` in this case, which in its drawRect function applies a time-dependent `CIFilter` on a `CIImage`. The problem is that it runs synchronous and the second animation starts right after end of the first. Is there a way to run them simultaneously?