is there a way to run 2 NSAnimation objects simultaneously?
Asked Answered
E

1

8

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?

Ento answered 15/4, 2012 at 21:56 Comment(1)
Where's the second animation? And in what way did things not work?Nadabus
G
19

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.

Grosso answered 16/4, 2012 at 4:49 Comment(12)
Thanks a lot! I wasn't aware of this one. Somehow i didn't reach it in google search. Will try it and let you know.Ento
I'm just curious: If we're having performance (low frame rate) issues with NSAnimation, would applying this protocol possibly help?Amendment
That depends what's causing the low frame rate. The animation proxy isn't a magic bullet and will still make your views draw, so if the drawing/compositing is slow then moving to this technology is not going to make a lot of difference. More likely is that you'll need to use a higher-performance rendering technology, such as Core Animation, which is specifically designed to render animation smoothly.Grosso
@RobKeniger: Thanks! We did end up switching to Core Animation on your suggestion and the performance isn't even comparable. Much better!Amendment
@RobKeniger while this approach looks promising, but it lacks the animation progress value, that is used in effects calculations. How can I obtain it? Though I can animate a custom property, but I'll have to connect its animation somehow with a media timing function. It doesn't seem to be intuitive...Is there a callback like setCurrentProgress that can be used? I couldn't find it in the documentation.Ento
You need to add a timing function to the animation, using the ‑setTimingFunction: method of NSAnimationContext. The timing function is a CAMediaTimingFunction. If you want to create a custom timing function you'll need to use the +functionWithControlPoints:::: method. This site might be helpful to you.Grosso
Specifically I'm talking about CIPerspectiveTransform, which is presented as a transition animation. It calculates the perspective, based on time value from timing function. Literally each time-step it changes the parameters of the transform filter to get the updated CIImage. The transition steps are incremental, so in the end it looks like a flip transition. I know how to connect the media timing function, but I have to access its values during the animation, so the new values can be calculated on fly. How do I do that?Ento
That's not how you do it. You just need to get your view to redraw when any of the animated properties change, and update whatever is in your view based on the current value of those properties. I have created a sample project so you can see how simple it is: github.com/rkeniger/Animating-ViewGrosso
I've updated the answer with more information about how to implement the protocol, as well as what you might do to get the current progress.Grosso
Got it... So I should add an animated properly "time" and in its setter call SetNeedsDisplay. On draw I should draw the CIImage with a perspective filter that is calculated with my "time" property. That's the flow? Many many thanks on your patience and fully detailed explanation!Ento
That's pretty much exactly it.Grosso
This is quite old but was very helpful to me so I wanted to pop in and say thanks!Desultory

© 2022 - 2024 — McMap. All rights reserved.