Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8674939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:49:45+00:00 2026-06-12T19:49:45+00:00

I have a custom view in an NSStatusItem object. This view displays icons. It’s

  • 0

I have a custom view in an NSStatusItem object.
This view displays icons. It’s also able to show a progress, but you have to call [self.statusitemview setProgressValue:theValue];
I have a set of icons, and it chooses the right one using this value.

This seems very jerky, because the executed process doesn’t send updates all the time.
So I would like to animate this.

I would like to call the animation like you can with other cocoa-controls:
[[self.statusItemView animator] setProgressValue:value];

If that’s at all possible

What is the proper way to do this?
I wouldn’t want to use an NSTimer.

EDIT

The images are drawn using the drawRect: method

Here’s the code:

- (void)drawRect:(NSRect)dirtyRect
{
    if (self.isHighlighted) {
        [self.statusItem drawStatusBarBackgroundInRect:self.bounds withHighlight:YES];
    }

    [self drawIcon];
}

- (void)drawIcon {
    if (!self.showsProgress) {
        [self drawIconWithName:@"statusItem"];
    } else {
        [self drawProgressIcon];
    }
}

- (void)drawProgressIcon {
    NSString *pushed = (self.isHighlighted)?@"-pushed":@"";
    int iconValue = ((self.progressValue / (float)kStatusItemViewMaxValue) * kStatusItemViewProgressStates);
    [self drawIconWithName:[NSString stringWithFormat:@"statusItem%@-%d", pushed, iconValue]];
}

- (void)drawIconWithName:(NSString *)iconName {
    if (self.isHighlighted && !self.showsProgress) iconName = [iconName stringByAppendingString:@"-pushed"];
    NSImage *icon = [NSImage imageNamed:iconName];
    NSRect drawingRect = NSCenterRect(self.bounds, icon);

    [icon drawInRect:drawingRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:YES hints:nil];
}


- (void)setProgressValue:(int)progressValue {
    if (progressValue > kStatusItemViewMaxValue || progressValue < 0) {
        @throw [NSException exceptionWithName:@"Invalid Progress Value"
                                       reason:[NSString stringWithFormat:@"The value %d id invalid. Range {0 - %d}", progressValue, kStatusItemViewMaxValue]
                                     userInfo:nil];
    }

    _progressValue = progressValue;
    [self setNeedsDisplay:YES];
}

- (void)setShowsProgress:(BOOL)showsProgress {
    if (!showsProgress) self.progressValue = 0;
    _showsProgress = showsProgress;

    [self setNeedsDisplay:YES];
}

It has to be possible somehow.
Since standard controls from Apple are drawn using the drawRect:, but have smooth animations…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:49:47+00:00Added an answer on June 12, 2026 at 7:49 pm

    To animate custom properties, you need to make your view conform to the NSAnimatablePropertyContainer protocol.

    You can then set up multiple custom properties as animatable (in addition to the properties already supported by NSView), and then you can simply use your views’ animator proxy to animate the properties:

    yourObject.animator.propertyName = finalPropertyValue;
    

    Apart from making animation very simple, it also allows you to animate multiple objects simultaneously using an NSAnimationContext:

    [NSAnimationContext beginGrouping];
    firstObject.animator.propertyName = finalPropertyValue1;
    secondObject.animator.propertyName = finalPropertyValue2;
    [NSAnimationContext endGrouping];
    

    You can also set the duration and supply a completion handler block:

    [NSAnimationContext beginGrouping];
    [[NSAnimationContext currentContext] setDuration:0.5];
    [[NSAnimationContext currentContext] setCompletionHandler:^{
        NSLog(@"animation finished");
    }];
    firstObject.animator.propertyName = finalPropertyValue1;
    secondObject.animator.propertyName = finalPropertyValue2;
    [NSAnimationContext endGrouping];
    

    For a standard NSView object, if you want to add animation support to a property in your view, you just need to override the +defaultAnimationForKey: method in your view and return an animation for the property:

    //declare the default animations for any keys we want to animate
    + (id)defaultAnimationForKey:(NSString *)key
    {
        //in this case, we want to add animation for our x and y keys
        if ([key isEqualToString:@"x"] || [key isEqualToString:@"y"]) {
            return [CABasicAnimation animation];
        } else {
            // Defer to super's implementation for any keys we don't specifically handle.
            return [super defaultAnimationForKey:key];
        }
    }
    

    I’ve created a simple sample project that shows how to animate multiple properties of a view simultaneously using the NSAnimatablePropertyContainer protocol.

    All your view needs to do to update successfully is make sure that setNeedsDisplay:YES is called when any of the animatable properties are modified. You can then get the values of those properties in your drawRect: method and update the animation based on those values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom view, that has page and block. But the problem when
I have an NSStatusItem that has an NSMenuItem which contains a custom NSView. this
Currently, I have an NSStatusItem that, when clicked, shows a custom view below it.
I have a custom view in my layout and I also have a TextView
I have a custom view which I call Node that is a child of
I have created custom View called Color . I use object of Color to
I have a custom View that I would like to embed in an WebView.
I have a custom view which has a piano keyboard drawn inside of it.
I have a custom view (inherited from UIView ) in my app. The custom
I have a custom view: public class Loading extends View { private long movieStart;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.