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

  • Home
  • SEARCH
  • 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 7594247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:22:07+00:00 2026-05-30T21:22:07+00:00

I am making a custom NSView object that has some content that changes often,

  • 0

I am making a custom NSView object that has some content that changes often, and some that changes much more infrequently. As it would turn out, the parts that change less often take the most time to draw. What I would like to do is render these two parts in different layers, so that I can update one or the other separately, thus sparing my user a sluggish user interface.

How might I go about doing this? I have not found many good tutorials on this sort of thing, and none that talk about rendering NSBezierPaths on a CALayer. Ideas anyone?

  • 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-05-30T21:22:09+00:00Added an answer on May 30, 2026 at 9:22 pm

    Your hunch is right, this is actually an excellent way to optimise drawing. I’ve done it myself where I had some large static backgrounds that I wanted to avoid redrawing when elements moved on top.

    All you need to do is add CALayer objects for each of the content items in your view. To draw the layers, you should set your view as the delegate for each layer and then implement the drawLayer:inContext: method.

    In that method you just draw the content of each layer:

    - (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx
    {
        if(layer == yourBackgroundLayer)
        {   
            //draw your background content in the context
            //you can either use Quartz drawing directly in the CGContextRef,
            //or if you want to use the Cocoa drawing objects you can do this:
            NSGraphicsContext* drawingContext = [NSGraphicsContext graphicsContextWithGraphicsPort:ctx flipped:YES];
            NSGraphicsContext* previousContext = [NSGraphicsContext currentContext];
            [NSGraphicsContext setCurrentContext:drawingContext];
            [NSGraphicsContext saveGraphicsState];
            //draw some stuff with NSBezierPath etc
            [NSGraphicsContext restoreGraphicsState];
            [NSGraphicsContext setCurrentContext:previousContext];
        }
        else if (layer == someOtherLayer)
        {
            //draw other layer
        }
        //etc etc
    }
    

    When you want to update the content of one of the layers, just call [yourLayer setNeedsDisplay]. This will then call the delegate method above to provide the updated content of the layer.

    Note that by default, when you change the layer content, Core Animation provides a nice fade transition for the new content. However, if you’re handling the drawing yourself you probably don’t want this, so in order to prevent the default fade in animation when the layer content changes, you also have to implement the actionForLayer:forKey: delegate method and prevent the animation by returning a null action:

    - (id<CAAction>)actionForLayer:(CALayer*)layer forKey:(NSString*)key 
    {
        if(layer == someLayer)
        {
            //we don't want to animate new content in and out
            if([key isEqualToString:@"contents"])
            {
                return (id<CAAction>)[NSNull null];
            }
        }
        //the default action for everything else
        return nil;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a custom form object in Django which has an overrided __init__
I am making an application that does some custom image processing. The program will
I am making a custom view that has 2 elemnts: a background that consists
I'm considering making a custom TabControl for the Compact Framework 3.5, one that has
I'm making a custom control that can be dragged around and it is semi
I am making a custom control I need to add some default keybindings, microsoft
Long story short, I'm making a custom Swing component that's basically a JTable with
I'm making a custom control that's an expandable/collapsable panel, which I can then fill
I'm making some custom control. One of the subviews of the control is transparent
I'm making a custom ListCellRenderer. I know that you can have different dimensions for

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.