仅在特定值上停止NSScrollView

仅在特定值上停止NSScrollView

本文介绍了仅在特定值上停止NSScrollView-类似于UIScrollView分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SDK 10.7作为部署目标的Mac OS X应用程序. NSScrollView包含图像缩略图的水平列表. ScrollView中心的缩略图指示选定的图像,该图像以完整尺寸显示在ScrollView的下方.这与Finder Cover-Flow十分相似,除了我的应用程序中的图像不会流动,而只是滚动开.

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.

我想限制滚动仅在缩略图正好在中间时才停止. NSView方法 adjustScroll:不是解决方案.这将禁用平滑"滚动,并使ScrollView从一个缩略图跳到下一个缩略图.

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.

我需要某种操作/回调,它告诉我ScrollView已完成滚动.然后我无法检查位置是否正确(如果缩略图在中间)或向后/向前滚动到最近的缩略图.但是NSScrollView不提供这样的回调.

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.

我试图观察ScrollViews contenView框架的变化.每当框架改变时,我都会启动一个计时器.如果计时器在触发前没有重新启动,我认为滚动停止了.这不是很好.如果滚动仅在短时间内停止(例如,因为移动了手指以进行另一次滑动手势),则下一次滚动会干扰位置的校正.

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.

在iOS下使用UIScrollView可以毫无问题地解决此任务. NSScrollView的类似分页功能将是一个完美的解决方案.

Using UIScrollView under iOS solves this task without any problem. A similar paging feature for NSScrollView would be a perfect solution.

有人知道如何解决吗?

非常感谢!

推荐答案

您可以使用 :

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)];
 }];

您可以使用 NSAnimationContext 来调整动画的持续时间和时间

You can use NSAnimationContext to tune the duration and timing of the animation.

注意:根据文档,此通知是在10.9中引入的,因此它不是原始问题中提到的10.7部署目标的选项.

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.

这篇关于仅在特定值上停止NSScrollView-类似于UIScrollView分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 06:15