【问题标题】:How to stop the animation on a determinate NSProgressIndicator?如何在确定的 NSProgressIndicator 上停止动画?
【发布时间】:2011-02-09 15:56:50
【问题描述】:

NSProgressIndicator 在isIndeterminate == YES 时允许stopAnimation:,但是如何停止确定进度条的动画?

对于上下文,我尝试使用的进度条是 NSView 的子项,它本身是 NSMenuItem 的 view 属性。向setAnimationDelay: 发送高得离谱的数字可以满足我的要求,但这只是暂时的——当父菜单关闭并重新打开时,进度条会再次动画。

(可能不必要的免责声明:我发誓这是一个合法的用例;我必须能够直观地(即:不使用文本)显示长时间运行的任务的进度,这可能根据后端的需要暂停并重新启动。除非附有出色的替代建议,否则不会接受归结为“UI 设计:您的成功”的答案。;))

【问题讨论】:

标签: cocoa animation nsprogressindicator


【解决方案1】:

像这样子类 NSProgressIndicator,它也适用于 Lion:

@interface UnanimatedProgressIndicator : NSProgressIndicator {
@private
    BOOL    isAnimating;
}
@end

@interface NSProgressIndicator (HeartBeat)
- (void) heartBeat:(id)sender;      // Apple internal method for the animation
@end

@implementation UnanimatedProgressIndicator

- (void) startAnimation:(id)sender
{
    isAnimating = YES;
    [super startAnimation:sender];
}

- (void) stopAnimation:(id)sender
{
    isAnimating = NO;
    [super stopAnimation:sender];
}

- (void) heartBeat:(id)sender
{
    if (isAnimating)
        [super heartBeat:sender];
}

@end

【讨论】:

  • 似乎不起作用 - 我已经尝试逐字使用,但 heartBeat: 似乎从未被调用过。
  • 我喜欢 Apple 在如此简单的事情上创造复杂性。
【解决方案2】:

使用与前面描述的相同方法解决了这个问题,只是做了一点小改动。

当菜单的代表收到menuNeedsUpdate: 时,我将setAnimationDelay:(以足够大的数字作为参数)发送到每个进度条。现在它运行可靠,所以我很满意。

【讨论】:

  • 此方法 setAnimationDelay: 在 10.5 中已弃用,似乎在 Lion 上根本不起作用。我不确定现在如何做到这一点。
  • 使用绑定来设置延迟时,它可以毫无怨言地工作。不过我没有 Lion,所以我不知道这是否可以在那里工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-27
  • 1970-01-01
相关资源
最近更新 更多