有没有办法同时运行2 NSAnimation对象? [英] is there a way to run 2 NSAnimation objects simultaneously?

查看:255
本文介绍了有没有办法同时运行2 NSAnimation对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用另一个视图创建了2个 NSAnimation 对象。我想同时运行2个这些动画。我不能使用 NSViewAnimation ,因为它现在是关于任何视图属性的动画。

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.

这里是动画创建: / p>

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];

我试图链接2个动画,但可能是因为某些原因没有工作。我举了一个例子:
Apple开发者网站

I tried to link 2 animations, but probably it didn't work for some reason. I took an example from: Apple developer site

配置 NSAnimation 对象使用 NSAnimationNonblocking 根本不显示任何动画...

configuring the NSAnimation object to use NSAnimationNonblocking doesn't show any animation at all...

编辑:第二个动画与第一个动画完全相同

The second animation is exactly the same as the first one and created in the same place the first is created.

TransitionAnimation NSAnimation ,其中 setCurrentProgress 看起来像这样:

TransitionAnimation is a subclass of NSAnimation, where the setCurrentProgress looks like that:

- (void)setCurrentProgress:(NSAnimationProgress)progress {
    [super setCurrentProgress:progress];
    [(NSView *)[self delegate] display];    
}

委托 NSView 在这种情况下,它的drawRect函数在上应用依赖于时间的 CIFilter CIImage 。问题是它运行同步,第二个动画从第一个动画结束后开始。是否有同时运行它们的方法?

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?

推荐答案

NSAnimation

相反,你应该让你的视图符合 NSAnimatablePropertyContainer 协议。

Instead, you should make your views conform to the NSAnimatablePropertyContainer protocol.

然后,您可以将多个自定义属性设置为animatable(除了 NSView 已支持的属性),然后您可以简单地使用视图' animator 代理动画的属性:

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;

除了使动画非常简单外,它还允许您使用 NSAnimationContext

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];

NSAnimation NSViewAnimation 类比animator代理支持更老,我强烈建议您尽可能远离它们。支持 NSAnimatablePropertyContainer 协议比管理所有 NSAnimation 委托简单得多。

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.

对于标准 NSView object,如果你想为视图中的属性添加动画支持,你只需要重写视图中的 + defaultAnimationForKey:方法,并返回一个动画属性:

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];
    }
}

我创建了一个简单的示例项目,显示如何使用 NSAnimatablePropertyContainer 协议同时为视图的多个属性设置动画。

I've created a simple sample project that shows how to animate multiple properties of a view simultaneously using the NSAnimatablePropertyContainer protocol.

您的所有视图需要做的更新成功是确保 setNeedsDisplay:YES 动画属性被修改。然后,您可以在 drawRect:方法中获取这些属性的值,并根据这些值更新动画。

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.

如果你想要一个类似于 NSAnimation 的简单进度值,你可以定义一个 progress 属性,然后执行这样的操作:

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];

然后,您可以访问 self.progress drawRect:方法来查找动画的当前值。

You can then access self.progress in your drawRect: method to find out the current value of the animation.

这篇关于有没有办法同时运行2 NSAnimation对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆