2012-04-15 90 views
7

我創建了2 NSAnimation用另一種視圖翻轉視圖的對象。我想同時運行2個這些動畫。我不能使用NSViewAnimation,因爲它現在是關於動畫任何視圖屬性的。有沒有辦法同時運行2個NSAnimation對象?

這裏是動漫創作:

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

[self.animation startAnimation]; 

我嘗試連結2級的動畫,但可能並沒有出於某種原因。我花了一個例子來自: Apple developer site

配置NSAnimation對象使用NSAnimationNonblocking不顯示任何動畫在所有...

編輯:第二個動畫是完全一樣的第一個和創建第一個創建的地方相同。

TransitionAnimationNSAnimation,其中setCurrentProgress看起來像一個子類:

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

delegate在這種情況下,在其的drawRect函數應用NSView時間依賴於CIImageCIFilter。問題是它同步運行,第二個動畫在第一個動畫結束後立即開始。有沒有辦法同時運行它們?

+1

第二部動畫在哪裏?什麼方法不起作用? – 2012-04-15 23:01:05

回答

17

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

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

我創建了一個簡單的sample project,演示瞭如何使用NSAnimatablePropertyContainer協議同時動畫化視圖的多個屬性。

所有視圖需要做成功更新是確保在任何動畫的屬性被修改setNeedsDisplay:YES被調用。然後,您可以在drawRect:方法中獲取這些屬性的值,並根據這些值更新動畫。

如果你想要一個簡單的進度值是類似的事情NSAnimation的工作方式,你可以定義你的視圖progress屬性,然後做這樣的事情:

yourView.progress = 0; 
[yourView.animator setProgress:1.0]; 

然後,您可以訪問self.progress在你的drawRect:方法中找出動畫的當前值。

+0

非常感謝!我不知道這一個。不知何故,我沒有達到谷歌搜索。將嘗試並讓你知道。 – 2012-04-16 05:36:46

+0

我只是很好奇:如果我們有NSAnimation的性能(低幀率)問題,應用這個協議可能有幫助嗎? – Zakman411 2012-05-09 08:18:42

+0

這取決於什麼導致低幀率。動畫代理不是一個神奇的子彈,它仍然會讓你的視圖畫出來,所以如果繪圖/合成速度很慢,那麼轉向這項技術不會產生很大的不同。更可能的是,您需要使用更高性能的渲染技術,例如Core Animation,它專門用於平滑渲染動畫。 – 2012-05-10 00:13:13

相關問題