Design Challenge: Core Animation, NSCollectionView, and NSCollectionViewItems


I'm struggling with a design problem: I have an NSCollectionView that contains several elements (bound to an NSArrayController , which in turn is bound to an NSManagedObjectContext ). I decided to draw a view for each individual element programmatically, mainly because I noticed that nesting several NSView inside an element view creates performance problems when there are more than a certain number of elements in the collection view.
Consider the following hierarchy: NSCollectionView => NSCollectionViewItem โ†’ NSView . (The default is NSCollectionView ). My custom NSView contains several layers, some with CATextLayer s, others with regular CALayer s, and they all wait together (within the same CATransaction ) when they need to. The problem here is that each CALayer must display the contents of some data available through the representedObject property of NSCollectionViewItem ..., which owns NSView ! I have two options (perhaps much more, I'm more than open to suggestions):
- I am replicating representedObject from NSCollectionViewItem to NSView , and I support it according to the execution of the program. I donโ€™t really like it.
- I open CALayer in NSView , and I set their contents / line from the NSCollectionViewItem setRepresentedObject: method. I like it better because there is no data stored in NSView (except what is shown through the layers, of course).
Am I mistaken? Is there a more elegant solution?

Thanks in advance for your help. Greetings

Jan Marco

+4
source share
1 answer

This question went through quite late, and I thought it was an interesting design problem.

If you havenโ€™t cracked it or liked any of your solutions:

I would suggest writing a subclass of NSCollectionViewItem that observes the key path self.representedObject and updates its presentation and subview when the model object changes.

NSCollectionViewItem is an NSViewController that implies that it must be responsible for managing its views. You can respect this by having the presentation layer with which it works as a representation of the model object that is facing the user contained in the property of the presented object. Thus, observing all the necessary properties of the model using KVO, you should have a neat place to invoke all the viewing operations related to the "rendering" of the model. In this sequence, you should have full control over whether you want to work with the NSView API or the CALayer API.

Depending on how difficult the presentation of your prototype element is, the size and volatility of the element set, the requirements for the "liveliness" of the user interface and other factors, you can leave with a very coarse-grained observation or you need a very fine-grained one - I hope you're used to working with the API KVO!

I also want to hear what choices you made at the end.

+3
source

Source: https://habr.com/ru/post/1382936/


All Articles