Search code examples
macoscocoansscrollview

NSScrollView with massive contentView causing terrible performance on scroll


I'm trying to draw a large waveform/graph that is an NSView placed inside of a NSScrollView. That way, the user can scroll horizontally and parts of it at a time.

The waveform view can be very large (technically, infinitely wide).

If the entire waveform is drawn, the scrolling performance is unusable. I'm unsure why NSScrollView is attempting to redraw the entire view rather than just the visible rect - but I suspect I will need to implement this logic myself.

What is the most efficient way to implement this to have a good scrolling experience?


Solution

  • You could tile the contents of your scroll view into several small NSView instances, each one representing a small part of your graph and placed right beside one another.

    However, I am not sure to what extent the views that are clipped off-screen still consume resources and affect performance.

    A better apporach would be similar to the above, but relying on the built-in functionality of a Collection View. Its machinery definitely takes care of displaying only the cells that should be visible (not clipped by the scroll view).