On 10 Mar 2010, at 17:13, Mazen M. Abdel-Rahman wrote:

> Hi All,
> 
> I was able to write a simple calendar view that uses basic cocoa graphics to 
> draw directly on the view (NSBezierPath, etc.).  I actually use several 
> different paths for drawing the calendar (it's a weekly calendar) to allow 
> different horizontal line widths (hour, half hour, etc.).  The calendar view 
> is inside a scroll view - and is actually about 3 times longer than the view 
> window.   The main problem is that the scrolling is not smooth - and my 
> assumption is that it's because the NSBezier stroke functions  have to be 
> constantly called to render the calendar.
> 
> For something as relatively simple as this would moving to core animation - 
> i.e. trying to render the calendar on a layer instead (I am still trying to 
> learn core animation) add performance benefits?  And would it allow for 
> smoother scrolling?
> 
As no one else has offered an opinion:

Moving to CA might offer benefits but you can improve the performance of your 
existing code by using an image cache.
I do this for an NSView with animated NSBezierPath content and it works fine.

So I would draw my image when the view resizes and there after service - 
(void)drawRect:(NSRect)rect from the image cache.
You only regen the cache when required, say on resize on content change.

The rough code outline below might help you get something up an running:

NSView subclass:

ivars:

NSRect _cacheRect;
NSImage *_imageCache;
BOOL _useImageCache;

- (void)drawRect:(NSRect)rect 
{       

        if (_useImageCache) {
                
                // validate our rect
                rect = [self validateDrawRect:rect];

                // if cache exists use it to update rect.
                // otherwise draw into our rect
                if (_imageCache) {
                        [self drawRectFromCache:rect];
                        return;
                } 

                // draw to image cache
                _cacheRect  = [self bounds];
                _imageCache = [[NSImage alloc] initWithSize:_cacheRect.size];
                [_imageCache lockFocus];
                
        }
        
        // draw entire bounds rect
        rect = [self bounds];
        
        // draw it
        NSBezierPath *bgPath = [NSBezierPath bezierPathWithRect:rect];
        NSColor *endColor = [NSColor colorWithCalibratedRed:0.988f green:0.988f 
blue:0.988f alpha:1.0f];
        NSColor *startColor = [NSColor colorWithCalibratedRed:0.875f 
green:0.875f blue:0.875f alpha:1.0f];
                
        NSGradient *gradient = [[NSGradient alloc] 
initWithStartingColor:startColor endingColor:endColor];
        [gradient drawInBezierPath:bgPath angle:90.0f];
        
        
        if (_useImageCache) {
                [_imageCache unlockFocus];
                
                // refresh view from cache
                [self drawRectFromCache:rect];
        }
        
}

/*
 
 draw rect from cache
 
 */
- (void)drawRectFromCache:(NSRect)rect
{
        [_imageCache drawInRect:rect fromRect:rect 
operation:NSCompositeSourceOver fraction:1.0f];
}
/*
 
 validate the draw rect
 
 */
- (NSRect)validateDrawRect:(NSRect)rect
{
        NSRect boundsRect = [self bounds];
        
        // if bounds rect and cache rect are not equal then
        // the cache will have to be updated
        if (!NSEqualRects(boundsRect, _cacheRect)) {
                [self clearDisplayCache];
        }
        
        // if no display cache available then need to draw bounds into cache
        if (!_imageCache) {
                rect = boundsRect;
        }
        
        return rect;
}

Regards

Jonathan Mitchell

Developer
http://www.mugginsoft.com

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to