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?
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.
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.
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With