Can I use CALayer to speed up rendering rendering?

I am creating a custom NSView object that has certain content that changes frequently, and some of which change much less frequently. As it turned out, parts that change less often take longer to draw. I would like to make these two parts in different layers so that I can update one or the other separately, thereby saving the user from a sluggish user interface.

How can i do this? I have not found many good guides on such things, and no one talks about how NSBezierPaths on CALayer. Anyone ideas?

+5
source share
1 answer

, . , , , .

, , CALayer . , , drawLayer:inContext:.

:

- (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
}

, [yourLayer setNeedsDisplay]. , .

, Core Animation . , , , , , , , actionForLayer:forKey: :

- (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;
}
+4

All Articles