Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add animated icon to OS X status bar?

I want to put an icon in Mac OS status bar as part of my cocoa application. What I do right now is:

NSStatusBar *bar = [NSStatusBar systemStatusBar];

sbItem = [bar statusItemWithLength:NSVariableStatusItemLength];
[sbItem retain];

[sbItem setImage:[NSImage imageNamed:@"Taski_bar_icon.png"]];
[sbItem setHighlightMode:YES];
[sbItem setAction:@selector(stopStart)];

but if I want the icon to be animated (3-4 frames), how do I do it?

like image 677
kender Avatar asked Jul 07 '11 14:07

kender


People also ask

How do I customize my Mac taskbar?

Change what's in the toolbar: Choose View > Customize Toolbar in the menu bar. You can drag items into and out of the toolbar, add a space between items, and choose whether to show text with the icons. Rearrange the items in the toolbar: Press and hold the Command key, then drag an item to a new location.

What is the top bar on a Mac called?

The menu bar runs along the top of the screen on your Mac. Use the menus and icons in the menu bar to choose commands, perform tasks, and check status.


1 Answers

You'll need to repeatedly call -setImage: on your NSStatusItem, passing in a different image each time. The easiest way to do this would be with an NSTimer and an instance variable to store the current frame of the animation.

Something like this:

/*

assume these instance variables are defined:

NSInteger currentFrame;
NSTimer* animTimer;

*/

- (void)startAnimating
{
    currentFrame = 0;
    animTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/30.0 target:self selector:@selector(updateImage:) userInfo:nil repeats:YES];
}

- (void)stopAnimating
{
    [animTimer invalidate];
}

- (void)updateImage:(NSTimer*)timer
{
    //get the image for the current frame
    NSImage* image = [NSImage imageNamed:[NSString stringWithFormat:@"image%d",currentFrame]];
    [statusBarItem setImage:image];
    currentFrame++;
    if (currentFrame % 4 == 0) {
        currentFrame = 0;
    }
}
like image 103
Rob Keniger Avatar answered Nov 01 '22 18:11

Rob Keniger