Created
January 4, 2012 05:37
-
-
Save kgn/1558664 to your computer and use it in GitHub Desktop.
Animated NSTableView Scrolling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Thanks to CuriousKea for getting this started! | |
// http://stackoverflow.com/a/8480325/239380 | |
- (void)scrollRowToVisible:(NSInteger)rowIndex animate:(BOOL)animate{ | |
if(animate){ | |
NSRect rowRect = [self rectOfRow:rowIndex]; | |
NSPoint scrollOrigin = rowRect.origin; | |
NSClipView *clipView = (NSClipView *)[self superview]; | |
scrollOrigin.y += MAX(0, round((NSHeight(rowRect)-NSHeight(clipView.frame))*0.5f)); | |
NSScrollView *scrollView = (NSScrollView *)[clipView superview]; | |
if([scrollView respondsToSelector:@selector(flashScrollers)]){ | |
[scrollView flashScrollers]; | |
} | |
[[clipView animator] setBoundsOrigin:scrollOrigin]; | |
}else{ | |
[self scrollRowToVisible:rowIndex]; | |
} | |
} |
Swift 5.3 version
extension NSTableView {
func scrollRowToVisible(row: Int, animated: Bool) {
if animated {
let rowRect = self.rect(ofRow: row)
var scrollOrigin = rowRect.origin
let clipView = self.superview as? NSClipView
let tableHalfHeight = NSHeight(clipView!.frame)*0.5
let rowRectHalfHeight = NSHeight(rowRect)*0.5
scrollOrigin.y = (scrollOrigin.y - tableHalfHeight) + rowRectHalfHeight
let scrollView = clipView!.superview as? NSScrollView
if scrollView!.responds(to: #selector(NSScrollView.flashScrollers)) {
scrollView!.flashScrollers()
}
clipView!.animator().setBoundsOrigin(scrollOrigin)
} else {
self.scrollRowToVisible(row)
}
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! This was helpful - below is the swift version :)
`func scrollRowToVisible(row: Int, animated: Bool) {
`