Daniel Jette
Daniel Jette

Reputation: 943

How can I disable the animations in an NSCollectionView

I would like to turn off the 'shuffle' animations that happen when you resize an NSCollectionView. Is this possible?

Upvotes: 9

Views: 2669

Answers (5)

Ky -
Ky -

Reputation: 32093

To disable all the collection view's animations in Swift, do this just before the something animatable happens:

NSAnimationContext.current.duration = 0

Upvotes: 2

Peter
Peter

Reputation: 1031

I was only able to get this to work if I did the following:

1) Subclass the view that the NSCollectionViewItem used as its view. That subclassed view required a CALayer and I set the view subclass as the delegate of the CALayer.

2) Implement the CALayer delegate method so no animation actions should occur:

override func actionForLayer(layer: CALayer, forKey event: String) -> CAAction? {
        return NSNull()
}

3) Finally, in the NSCollectionView data source method:

func collectionView(collectionView: NSCollectionView, itemForRepresentedObjectAtIndexPath indexPath: NSIndexPath) -> NSCollectionViewItem {

    // get a new collection view item
    ....

    // disable animations
    CATransaction.begin()
    CATransaction.setDisableActions(true)

    // populate your cell
    ....

    CATransaction.commit()    
}

Upvotes: 0

TheSavage
TheSavage

Reputation: 279

kainjow is correct. adding this:

- (id) animationForKey:(NSString *) key
{
    return nil;
}

to the prototype view subclass (not the collection view!) disables animations

Upvotes: 3

Padraig
Padraig

Reputation: 1569

This works, but it's setting a private instance variable so it may no be ok in the Mac App Store.

[collectionView setValue:@(0) forKey:@"_animationDuration"];

Upvotes: 5

kainjow
kainjow

Reputation: 4716

For 10.6, I was able to disable the animation by subclassing NSView, overriding animationForKey: and returning nil. Then make sure you use that view for the prototype's view.

Upvotes: 2

Related Questions