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

Abi*_*ern 12 cocoa animation objective-c

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

我想离开这个:

**********************************
*                                *
*--------------------------------*
*|                              |*
*|        view 1                |*
*|                              |*
*--------------------------------*
*                                *
**********************************
Run Code Online (Sandbox Code Playgroud)

对此:

**********************************
*                                *
*--------------------------------*
*|        view 2                |*
*--------------------------------*
*--------------------------------*
*|        view 1                |*
*--------------------------------*
*                                *
**********************************
Run Code Online (Sandbox Code Playgroud)

我不一定在寻找代码,从哪里开始的想法将不胜感激.

这适用于桌面应用.

Cra*_*tis 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];
Run Code Online (Sandbox Code Playgroud)

稍后,您可以使用以下命令返回单个视图:

[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];
Run Code Online (Sandbox Code Playgroud)

编辑:以上代码适用于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];
Run Code Online (Sandbox Code Playgroud)

  • UIKit仅适用于iPhone,而不适用于Mac.在Mac上,您需要使用NSAnimationContext进行分组,并使用eachView.animator.frame而不是eachView.frame. (2认同)