I am working on a Mac OS X app using SDK 10.7 as deployment target. A NSScrollView contains a horizontal list of image thumbnails. The thumbnail which is in the center of the ScrollView indicates the selected image which is shown below the ScrollView in full size. This works quite similar the the Finder Cover-Flow, beside that the images in my app do not flow but just scroll away.
I would like to limit the scrolling to stop only when a thumbnail is exactly in the center. The NSView method adjustScroll: is no solution. This would disable "smooth" scrolling and let the ScrollView jump from one thumbnail to the next.
I would need some kind of action/callback which tells me that the ScrollView finished scrolling. I could than check if the position is OK (if a thumbnail is in the center) or scroll back/forward to the closest thumbnail. But NSScrollView does not provide such an callback.
I tried to observe changes of the frame of the ScrollViews contenView. Evertime the frame changes I start a timer. If the timer is not restarted before it fires I assume that scrolling stopped. This does not work very well. If scrolling stopps only for a short time (e.g. because the finger is moved to do another swipe gesture) the next scrolling interferes with the correction of the position.
Using UIScrollView under iOS solves this task without any problem. A similar paging feature for NSScrollView would be a perfect solution.
Does any one know how to solve this?
Thank you very much!
You can use NSScrollViewDidEndLiveScrollNotification
:
NSScrollView* scrollView;
const CGFloat interval = 100;
[[NSNotificationCenter defaultCenter]
addObserverForName:NSScrollViewDidEndLiveScrollNotification
object:scrollView
queue:nil
usingBlock:^(NSNotification* notification) {
CGFloat offset = scrollView.contentView.bounds.origin.y;
offset = round(offset / interval) * interval;
[scrollView.contentView.animator setBoundsOrigin:NSMakePoint(0, offset)];
}];
You can use NSAnimationContext
to tune the duration and timing of the animation.
Note: According to the docs, this notification was introduced in 10.9, so it wouldn't have been an option for the 10.7 deployment target mentioned in the original question.
来源:https://stackoverflow.com/questions/14609793/stop-nsscrollview-only-on-specific-values-like-uiscrollview-paging