NSAnimation
对于同时动画多个对象及其属性并不是真正的最佳选择。
相反,您应该使您的意见符合NSAnimatablePropertyContainer
协议。
然后,您可以设置多个自定义属性为动画(除由NSView
已经支持的属性),然后你可以简单地使用你的意见animator
代理动画属性:
yourObject.animator.propertyName = finalPropertyValue;
除了从制作动画非常简单,它也可以让你的动画多个对象同时使用NSAnimationContext
:
[NSAnimationContext beginGrouping];
firstObject.animator.propertyName = finalPropertyValue1;
secondObject.animator.propertyName = finalPropertyValue2;
[NSAnimationContext endGrouping];
您还可以设置时间,并提供一个完成处理BLO CK:
[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
类是比动画代理支持大年纪了,我强烈建议你如果可能的话从他们搬走。支持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];
}
}
我创建了一个简单的sample project,演示了如何使用NSAnimatablePropertyContainer
协议同时动画化视图的多个属性。
所有视图需要做成功更新是确保在任何动画的属性被修改setNeedsDisplay:YES
被调用。然后,您可以在drawRect:
方法中获取这些属性的值,并根据这些值更新动画。
如果你想要一个简单的进度值是类似的事情NSAnimation
的工作方式,你可以定义你的视图progress
属性,然后做这样的事情:
yourView.progress = 0;
[yourView.animator setProgress:1.0];
然后,您可以访问self.progress
在你的drawRect:
方法中找出动画的当前值。
第二部动画在哪里?什么方法不起作用? – 2012-04-15 23:01:05