ios UICollectionView:如何检测滚动何时停止
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14868269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UICollectionView: how to detect when scrolling has stopped
提问by George Armhold
I'm using a UICollectionView
to scroll through a set of thumbnails quickly. Once scrolling ends, I'd like to display a larger hi-res version of the current thumbnail.
我正在使用 aUICollectionView
快速滚动一组缩略图。滚动结束后,我想显示当前缩略图的更大高分辨率版本。
How can I detect when the user has completed scrolling? I do implement didEndDisplayingCell
, but that only tells me when a particular cell has scrolled off; it doesn't tell me when the scroll motion actually completes.
如何检测用户何时完成滚动?我确实实现了didEndDisplayingCell
,但这只会告诉我特定单元格何时滚动;它没有告诉我滚动运动何时真正完成。
回答by iDev
NS_CLASS_AVAILABLE_IOS(6_0) @interface UICollectionView : UIScrollView
UICollectionView
is a subclass of UIScrollView
. So if you have set the delegate and implemented UIScrollViewDelegate
, you should be able to detect this the same way as UIScrollView
.
UICollectionView
是 的子类UIScrollView
。因此,如果您已设置委托并已实现UIScrollViewDelegate
,您应该能够以与UIScrollView
.
For eg:-
例如:-
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
As per documentation, the above method should tell when the scroll view has ended decelerating the scrolling movement.
根据文档,上面的方法应该告诉滚动视图何时结束减速滚动移动。
回答by Abey M
Just to cover your bases you should implement both these UIScrollViewDelegate methods.
In some cases there may not be a deceleration (and scrollViewDidEndDecelerating
would not be called), for e.g., the page is fully scrolled in place. In those case do your update right there in scrollViewDidEndDragging
.
只是为了覆盖您的基础,您应该实现这两个 UIScrollViewDelegate 方法。在某些情况下,可能没有减速(并且scrollViewDidEndDecelerating
不会被调用),例如,页面完全滚动到位。在这种情况下,请在scrollViewDidEndDragging
.
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if (!decelerate) {
[self updateStuff];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self updateStuff];
}
回答by D6mi
An important fact to note here :
这里要注意一个重要的事实:
This method gets called on User initiated scrolls (i.e a Pan gesture)
在用户发起的滚动(即平移手势)上调用此方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
On the other hand, this one gets called on all manually (programatically) initiated scrolls (like scrollRectToVisible
or scrollToItemAtIndexPath
)
另一方面,在所有手动(以编程方式)启动的卷轴(如scrollRectToVisible
或scrollToItemAtIndexPath
)上调用该卷轴
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
回答by Mr Stanev
Swift 3 version:
斯威夫特 3 版本:
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
// Your code here
}
回答by drpawelo
Swift 3 version of Abey M and D6mi 's answers:
Swift 3 版本的 Abey M 和 D6mi 的答案:
When scroll is caused by user action
当滚动是由用户操作引起的
public func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if (!decelerate) {
//cause by user
print("SCROLL scrollViewDidEndDragging")
}
}
public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
//caused by user
print("SCROLL scrollViewDidEndDecelerating")
}
When scroll is caused by code action (programmatically): (like "scrollRectToVisible" or "scrollToItemAtIndexPath")
当滚动由代码操作(以编程方式)引起时:(如“scrollRectToVisible”或“scrollToItemAtIndexPath”)
public func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
//caused by code
print("SCROLL scrollViewDidEndScrollingAnimation")
}
Notes:
笔记:
- Put these functions in your UIScrollViewDelegate or UICollectionViewDelegate delegate.
- if you don't have a separate delegate, make your current class extend a UIScrollViewDelegate op top of your class file
- 将这些函数放在 UIScrollViewDelegate 或 UICollectionViewDelegate 委托中。
- 如果您没有单独的委托,请让您当前的类扩展类文件的 UIScrollViewDelegate op 顶部
.
.
open class MyClass: NSObject , UICollectionViewDelegate
and somewhere in your viewWillAppear make the class its own delegate
并在您的 viewWillAppear 中的某处使该类成为自己的委托
override open func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// ...
self.myScrollView.delegate = self
// ...
}
回答by Ofir Malachi
if you want to use the visible indexpath:
如果要使用可见的索引路径:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self scrollingFinish];
}
- (void)scrollingFinish {
if([self.collectionView indexPathsForVisibleSupplementaryElementsOfKind:UICollectionElementKindSectionHeader]){
NSIndexPath *firstVisibleIndexPath = [[self.collectionView indexPathsForVisibleSupplementaryElementsOfKind:UICollectionElementKindSectionHeader] firstObject];
[self.collectionView scrollToItemAtIndexPath:firstVisibleIndexPath atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
[NSObject cancelPreviousPerformRequestsWithTarget:self];
}
}