2009-04-07 71 views
12

我想要在窗口上顯示一個視圖並響應消息(按鈕單擊或菜單)我想讓另一個視圖在其上滑動,並調整第一個視圖。如何在Cocoa程序中將窗口滑入和滑出窗口

我想從這個去:

********************************** 
*        * 
*--------------------------------* 
*|        |* 
*|  view 1    |* 
*|        |* 
*--------------------------------* 
*        * 
********************************** 

這樣:

********************************** 
*        * 
*--------------------------------* 
*|  view 2    |* 
*--------------------------------* 
*--------------------------------* 
*|  view 1    |* 
*--------------------------------* 
*        * 
********************************** 

我不一定要找碼,從哪裏開始的想法,將不勝感激。

這是一個桌面應用程序。

回答

19

CoreAnimation絕對是您最好的選擇。它已經有一段時間,因爲我已經有任何CA碼的工作,但這樣的:

[UIView beginAnimations:@"slideOff" context:nil]; 

firstView.frame = normalFirstViewRect; // The rect defining the first view's normal frame. This should expand the first view. 

secondView.frame = secondViewOffScreenFrame; // Move the second view off the screen 

[UIView commitAnimations]; 

編輯:

[UIView beginAnimations:@"slideOn" context:nil]; 

firstView.frame = shrunkFirstViewRect; // The rect defining the first view's smaller frame. This should resize the first view 

secondView.frame = secondViewOnScreenFrame; // This should move the second view on the frame. 

[UIView commitAnimations]; 

之後,你可以使用恢復到一個單一的視圖上面的代碼適用於iPhone,我讀了一下你的問題。

在Mac上,你可能需要使用(下同):

[NSAnimationContext beginGrouping]; 
[[NSAnimationContext currentContext] setDuration:1.0f]; // However long you want the slide to take 

[[firstView animator] setFrame:shrunkFirstViewRect]; 

[[secondView animator] setFrame:secondViewOnScreenFrame]; 

[NSAnimationContext endGrouping]; 
+2

UIKit僅適用於iPhone,不適用於Mac。在Mac上,您需要使用NSAnimationContext進行分組,而使用每個View.animator.frame而不是每個View.frame。 – 2009-04-07 18:58:51

1

我從來沒有嘗試過,但我認爲CoreAnimation有這個有趣的功能。您必須將view1的高度從全高度設置爲高度的一半,並將view2的位置從其超視圖的外部移動到其上半部分。

2

,如果你沒有設置持續時間的動畫塊應當注意,默認爲約0.25秒,這實際上在大多數情況下似乎工作得很好。

我建議在試驗CoreAnimation時首先嚐試使用該持續時間。