Animated progress bar in NSMenuItem

∥☆過路亽.° 提交于 2019-12-08 08:51:28

The timer approach should be completely unnecessary. Have you told the NSProgressIndicator to -setUsesThreadedAnimation:YES then told it to -startAnimation:?

For an indeterminate indicator I send the startAnimation: message in the menu's menuWillOpen: delegate method using performSelector:... to send it in the NSEventTrackingRunLoopMode mode.

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

For the determinate indicator I tried your code (but in objective-C) and it worked. I don't know python or PyObjC but looking at the code for the time variable I think you are sending the timeIntervalSinceReferenceDate() call to the NSDate class. So maybe time is always zero? Based on the alloc() calls I wonder if it shouldn't be ...

time = NSDate.date().timeIntervalSinceReferenceDate()


Update: for the record here is the code I used for testing the determinate progress indicator. Just obj-c version of the OP's code. The indicator updates properly.

- (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)];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!