1

NSMenuItem我想在自定义视图中放置一个动画进度条。这在 Apple 的MenuItemView示例中得到了演示,但它没有动画(至少在 10.5 中没有,并且示例显然来自 10.4)。

我尝试设置一个调用 的计时器,按文档所说的那样setNeedsDisplay:YES安排。NSEventTrackingRunLoopMode这有效,但仅适用于确定的进度条,如果我更改值,并且仅在第一次打开菜单时。第二次和连续两次,条形图重绘两次,然后保持冻结状态。对于不确定的进度条,理发杆条纹从不动画。


编辑:代码片段。我刚刚添加了itemChanged调用,似乎没有任何效果。更新纯文本项目工作正常。

class AppDelegate(NSObject):
  barItem = None
  menuProgressBar = None
  progressItem = None

  def applicationDidFinishLaunching_(self, sender):
    statusbar = NSStatusBar.systemStatusBar()
    self.statusitem = statusbar.statusItemWithLength_(
        NSSquareStatusItemLength)
    self.statusitem.setHighlightMode_(True)
    image = NSImage.imageNamed_("menubar.png")
    self.statusitem.setImage_(image)
    self.statusitem.retain()

    menu = NSMenu.alloc().init()

    AppDelegate.barItem = NSMenuItem.alloc(). \
        initWithTitle_action_keyEquivalent_('progress', None, '')
    itemView = NSView.alloc().initWithFrame_(NSMakeRect(0, 0, 50, 20))
    itemView.setAutoresizingMask_(NSViewWidthSizable)
    AppDelegate.menuProgressBar = \
        NSProgressIndicator.alloc().initWithFrame_(NSMakeRect(16, 5, 22, 10))
    AppDelegate.menuProgressBar.setAutoresizingMask_(NSViewWidthSizable)
    AppDelegate.menuProgressBar.setControlSize_(NSSmallControlSize)
    AppDelegate.menuProgressBar.setUsesThreadedAnimation_(True)
    itemView.addSubview_(AppDelegate.menuProgressBar)
    AppDelegate.menuProgressBar.setIndeterminate_(False)
    AppDelegate.menuProgressBar.setMaxValue_(100)
    AppDelegate.menuProgressBar.startAnimation_(self)
    timer = NSTimer.timerWithTimeInterval_target_selector_userInfo_repeats_(
        0.1, self,
        objc.selector(self.animateProgress, signature='v@:'),
        None, True)
    NSRunLoop.currentRunLoop().addTimer_forMode_(
        timer, NSEventTrackingRunLoopMode)
    AppDelegate.barItem.setView_(itemView)
    menu.addItem_(AppDelegate.barItem)

    AppDelegate.progressItem = NSMenuItem.alloc(). \
        initWithTitle_action_keyEquivalent_('Progress', None, '')
    menu.addItem_(AppDelegate.progressItem)

    self.statusitem.setMenu_(menu)

  def animateProgress(self):
    time = NSDate.timeIntervalSinceReferenceDate()
    AppDelegate.menuProgressBar.setDoubleValue_(time%100)
    AppDelegate.menuProgressBar.display()
    AppDelegate.progressItem.setTitle_('Progress: %d'%(time%100))
    AppDelegate.barItem.menu().itemChanged_(AppDelegate.barItem)
4

2 回答 2

1

计时器方法应该是完全没有必要的。你有没有告诉 NSProgressIndicator -setUsesThreadedAnimation:YES 然后告诉它 -startAnimation:?

于 2010-11-03T18:14:39.833 回答
1

对于不确定的指标,我在菜单的 menuWillOpen: 委托方法中发送 startAnimation: 消息,使用 performSelector:... 在 NSEventTrackingRunLoopMode 模式下发送它。

- (void)menuWillOpen:(NSMenu *)menu
{
  [[progressIndicator performSelector:@selector(startAnimation:)
                           withObject:self
                           afterDelay:0.0
                              inModes:[NSArray arrayWithObject:NSEventTrackingRunLoopMode]];
}

对于确定的指标,我尝试了您的代码(但在 Objective-C 中)并且它有效。我不知道 python 或 PyObjC 但查看时间变量的代码我认为您正在将 timeIntervalSinceReferenceDate() 调用发送到 NSDate 类。所以也许时间总是零?基于 alloc() 调用,我想知道它是否不应该......

时间 = NSDate.date().timeIntervalSinceReferenceDate()


更新:这里记录的是我用于测试确定进度指示器的代码。只是 OP 代码的 obj-c 版本。指标正确更新。

- (void)menuWillOpen:(NSMenu *)menu
{
    NSTimer *timer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(animateProgress:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSEventTrackingRunLoopMode];
}

- (void)animateProgress:(NSTimer *)timer
{
    NSTimeInterval time = [[NSDate date] timeIntervalSinceReferenceDate];
    [progressIndicator setDoubleValue:fmod(time, 100)];
}
于 2010-11-05T03:42:47.607 回答