如何在Cocoa程序中将视图滑入和滑出窗口

12

我希望在一个窗口上有一个视图,并在响应消息(按钮点击或菜单)时使另一个视图向下滑动并使第一个视图调整大小。

我想从这个状态:

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

变成这个样子:

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

我并不是在寻找代码,只是希望能得到一个开始的方向。

这是为桌面应用程序而准备的。

4个回答

19

CoreAnimation绝对是您最好的选择。虽然我已经有一段时间没有使用CA代码了,但类似这样的东西:

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

之后,您可以使用以下方法返回到单个视图:

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

编辑:上面的代码是为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进行分组,并使用eachView.animator.frame而不是eachView.frame。 - Peter Hosey
好的,我没有注意到底部的注释。已更新答案,谢谢! - Craig Otis

2

需要注意的是,如果您没有为动画块设置持续时间,则默认持续时间约为0.25秒,在大多数情况下实际上似乎非常有效。

我建议在尝试CoreAnimation时首先使用该持续时间。


1

我从未尝试过,但我认为CoreAnimation对此有趣的功能。您需要将view1的高度从完整高度动画到一半高度,并将view2的位置从其父视图外部动画到其顶部一半。


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接