Search code examples
cocoazoomingpinchnscollectionview

NSCollectionView pinch zoom


I am trying to get pinch zoom in an NSCollectionView by implementing -(void)magnifyWithEvent:(NSEvent *)event, but I'm not sure on what to do inside this method. I'm getting the amount of the zoom with [event magnification], and now I need to refresh the items inside the collection view.

I thought that I could save this in a variable zoomValue, and override the method - (NSCollectionViewItem *)newItemForRepresentedObject:(id)object in order to resize the view of each collection item (I think that this will not work, though. And anyway I don't know how to trigger the "refresh" of a collection view).

Any ideas about this? Thank you.


Solution

  • I'm doing something similar, but from a slider instead of a pinch. You just need to call:

    [collectionView setMinItemSize:newSize];
    [collectionView setMaxItemSize:newSize];
    [collectionView setNeedsDisplay:YES];
    

    To get the resize to happen live during the drag of the slider, I had to call a private method -[NSCollectionView _updateGridWithCurrentItemsIfNecessary], otherwise the resize didn't happen until mouseup. Not sure if you'll have the same problem.