Stop NSScrollView only on specific values - Like UIScrollView paging

自闭症网瘾萝莉.ら 提交于 2019-12-10 01:39:57

问题


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!


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!