有没有办法同时运行2个NSAnimation对象?

Nav*_*mon 7 cocoa objective-c core-image osx-lion nsanimation

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

这是动画创作:

self.animation = [[[TransitionAnimation alloc] initWithDuration:1.0 animationCurve:NSAnimationEaseInOut] autorelease];
[self.animation setDelegate:delegate];
[self.animation setCurrentProgress:0.0];

[self.animation startAnimation];
Run Code Online (Sandbox Code Playgroud)

我试图链接2个动画,但可能由于某种原因它不起作用.我举了一个例子: Apple开发者网站

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

编辑:第二个动画与第一个动画完全相同,并在创建第一个动画的同一个地方创建.

TransitionAnimation是一个子类NSAnimation,setCurrentProgress看起来像这样:

- (void)setCurrentProgress:(NSAnimationProgress)progress {
    [super setCurrentProgress:progress];
    [(NSView *)[self delegate] display];    
}
Run Code Online (Sandbox Code Playgroud)

delegateNSView在这种情况下,在其功能的drawRect施加时间依赖CIFilter于一个CIImage.问题是它运行同步,第二个动画在第一个动画结束后立即开始.有没有办法同时运行它们?

Rob*_*ger 17

NSAnimation 并不是同时动画多个对象及其属性的最佳选择.

相反,您应该使您的视图符合NSAnimatablePropertyContainer协议.

然后,您可以将多个自定义属性设置为动画(除了已支持的属性之外NSView),然后您只需使用视图的animator代理来设置属性的动画:

yourObject.animator.propertyName = finalPropertyValue;
Run Code Online (Sandbox Code Playgroud)

除了使动画非常简单之外,它还允许您使用以下方法同时为多个对象设置动画NSAnimationContext:

[NSAnimationContext beginGrouping];
firstObject.animator.propertyName = finalPropertyValue1;
secondObject.animator.propertyName = finalPropertyValue2;
[NSAnimationContext endGrouping];
Run Code Online (Sandbox Code Playgroud)

您还可以设置持续时间并提供完成处理程序块:

[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0.5];
[[NSAnimationContext currentContext] setCompletionHandler:^{
    NSLog(@"animation finished");
}];
firstObject.animator.propertyName = finalPropertyValue1;
secondObject.animator.propertyName = finalPropertyValue2;
[NSAnimationContext endGrouping];
Run Code Online (Sandbox Code Playgroud)

NSAnimationNSViewAnimation班比动画代理支持大年纪了,我强烈建议你如果可能的话从他们搬走.支持的NSAnimatablePropertyContainer协议是比管理所有的简单NSAnimation代表的东西.Lion支持自定义计时功能和完成处理程序意味着不再需要这样做了.

对于标准NSView对象,如果要向视图中的属性添加动画支持,则只需覆盖+defaultAnimationForKey:视图中的方法并返回属性的动画:

//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];
    }
}
Run Code Online (Sandbox Code Playgroud)

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

要成功更新所需的所有视图都要确保setNeedsDisplay:YES在修改任何可动画属性时调用它.然后,您可以在drawRect:方法中获取这些属性的值,并根据这些值更新动画.

如果您想要一个类似于工作方式的简单进度值NSAnimation,您可以progress在视图上定义属性,然后执行以下操作:

yourView.progress = 0;
[yourView.animator setProgress:1.0];
Run Code Online (Sandbox Code Playgroud)

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

  • 这已经很旧了,但对我很有帮助,所以我想进来说声谢谢! (2认同)