【问题标题】:How can I slide a view in and out of a window in a Cocoa program如何在 Cocoa 程序中将视图滑入和滑出窗口
【发布时间】:2009-04-07 17:30:51
【问题描述】:

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

我想从这里开始:

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

到这里:

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

我不一定要寻找代码,如果能知道从哪里开始,将不胜感激。

这是一个桌面应用程序。

【问题讨论】:

    标签: objective-c cocoa animation


    【解决方案1】:

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

    【讨论】:

    • UIKit 仅适用于 iPhone,不适用于 Mac。在 Mac 上,您需要使用 NSAnimationContext 进行分组,并且需要使用 eachView.animator.frame 而不是 eachView.frame。
    • 好电话,我没看到底部的注释。更新答案,谢谢!
    • 谢谢大家。我试试看。
    【解决方案2】:

    需要注意的是,如果不设置动画块的时长,默认为0.25秒左右,实际上在大多数情况下似乎效果很好。

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

    【讨论】:

      【解决方案3】:

      我从未尝试过,但我认为 CoreAnimation 对此有一些有趣的功能。您必须将 view1 的高度从全高设置为半高,并将 view2 的位置从其父视图外部设置为上半部分。

      【讨论】:

        【解决方案4】:

        或者,您可以尝试NSSplitView...

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-02-05
          • 2013-02-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多