Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTableView scrollRowToVisible with animation

I am trying to implement an action to scroll to the top of a NSTableView, and the bottom of the NSTableView. I am using scrollRowToVisible but I'd love the action to be animated. Is there a way to do this?

like image 444
keegan3d Avatar asked Oct 28 '11 04:10

keegan3d


3 Answers

While the NSTableView does not have a scroll property you can directly animate, you can instead, with a bit of math animate the scrolling of the NSClipView that the NSTableView lives in.

Here is how I did this (within a custom subclass of NSTableView) to smoothly animate the row at rowIndex to be scrolled to the center of the view, if possible:

        NSRect rowRect = [self rectOfRow:rowIndex];
        NSRect viewRect = [[self superview] frame];
        NSPoint scrollOrigin = rowRect.origin;
        scrollOrigin.y = scrollOrigin.y + (rowRect.size.height - viewRect.size.height) / 2;
        if (scrollOrigin.y < 0) scrollOrigin.y = 0;
        [[[self superview] animator] setBoundsOrigin:scrollOrigin];
like image 160
CuriousKea Avatar answered Oct 18 '22 20:10

CuriousKea


If you're targeting 10.8+ and your table view is layer backed, you can do this:

[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context){
    context.allowsImplicitAnimation = YES;
    [self.tableView scrollRowToVisible:someRow];
} completionHandler:NULL];
like image 39
pfandrade Avatar answered Oct 18 '22 18:10

pfandrade


It does not seem to be possible. NSTableView has not supported any kind of animations up to 10.6. Starting from MasOSX10.7 some simple animations added to the class. You can animate inserting, removing and moving rows to new positions. This is it so far.

like image 2
Davyd Geyl Avatar answered Oct 18 '22 19:10

Davyd Geyl