Question

I'm attempting to create a grid style view (similar to NSCollectionView), except using Core Animation. I'm pretty far through it, and the last major thing left to do is implement scrolling.

My setup so far is that I have an NSView subclass (layer backed), and upon initialization it creates and adds the grid layer as a sublayer of the main view layer. I have created a custom CALayoutManager for the grid layer that arranges its subviews in a grid-like formation. As expected, when I add sublayers to the grid layer, the layout manager is called and the layers are positioned automatically. Up to this point, everything is working as it should.

The problem comes when I try to use an NSScrollView as a parent of my custom view to implement scrolling. I set this up as follows: I created my custom view as a subview of the NSScrollView in Interface Builder. Then, in my layout manager class, I added a delegate property and during initialization, my view subclass sets itself as the delegate of the layout manager. At the end of the layoutSublayersForLayer: method of the layout manager, I call upon its delegate with the delegate method layoutManager:contentHeightChanged:. Here is the implementation of that method in my NSView subclass:

- (void)layoutManager:(MyLayoutManager*)manager contentHeightChanged:(CGFloat)height;
{
    CGFloat newHeight = [[self enclosingScrollView] contentSize].height;
    if (height > newHeight) {
        newHeight = height;
    }
    NSRect newFrame = [self frame];
    newFrame.size.height = newHeight;
    [self setFrame:newFrame];
}

It's pretty simple, it just checks to see whether the new height is larger than the content size of the scroll view, and sets the views frame with the new height.

This works — to a certain extent. When the view resizes, it sizes the view's frame properly as it should to encapsulate the full height of the content, thereby making scrollbars appear. The problem: the sublayers of the grid layer jitter when the view is being resized with the scrollbars visible. Here's a video showing the problem:

http://vimeo.com/16987653

As you can see, there is no issue when the scrollbars aren't visible (in other words, when the height of the content fits within the bounds of the scroll view). I can confirm that this isn't a problem with the layout manager and dealing with single columns, because I tested the same thing without the scroll view and there are no jitters.

Any advice is greatly appreciated.

Was it helpful?

Solution

Solved this problem by flipping the coordinate system of both the layer and the view (origin at top left corner).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top