2009-04-07 104 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时首先尝试使用该持续时间。