[英]UICollectionView - separating scrollViewDidScroll and scrollToItemAtIndexPath
My screen has a calendar view and below it a UICollectionView
with a list of events ordered by date. 我的屏幕上有一个日历视图,在它的下面有一个
UICollectionView
其中包含按日期排序的事件列表。
When the user changes the month in the calendar, I scroll the collection view to that month's events using UICollectionView scrollToItemAtIndexPath
. 当用户在日历中更改月份时,我将使用
UICollectionView scrollToItemAtIndexPath
将集合视图滚动到该月的事件。
When the user scrolls through the UICollectionView
, I use scrollViewDidScroll
and UICollectionView indexPathsForVisibleItems
to detect the scrolling and change the calendar to match the month for the events that are visible. 当用户滚动
UICollectionView
,我使用scrollViewDidScroll
和UICollectionView indexPathsForVisibleItems
来检测滚动并更改日历以匹配可见事件的月份。
Now, the problem is that changing the calendar month calls scrollToItemAtIndexPath
, which triggers scrollViewDidScroll
, which changes the calendar month, which... you get the idea. 现在,问题在于更改日历月会调用
scrollToItemAtIndexPath
,这会触发scrollViewDidScroll
,后者会更改日历月,从而...
Is there any way to separate the two calls? 有什么办法可以将两个电话分开吗? I guess what I want is to only call
scrollViewDidScroll
if it's triggered by the user scrolling, but I'm happy with any other solution that reaches my goal. 我想我想只在用户滚动触发它时才调用
scrollViewDidScroll
,但是我对达到我的目标的任何其他解决方案感到满意。
UICollectionView
inherits from UIScrollView
. UICollectionView
继承自UIScrollView
。 You can therefore access the tracking
property to determine "whether the user has touched the content to initiate scrolling." 因此,您可以访问
tracking
属性以确定“用户是否已触摸内容以启动滚动”。
The seemingly obtuse wording there refers to the fact that the user may have started touching but not yet started scrolling. 那里看似晦涩的用语是指用户可能已经开始触摸但尚未开始滚动的事实。 However if the view is scrolling without the user having started touching it then it's safe to assume a programmatic scroll.
但是,如果视图在滚动时没有用户开始触摸它,那么可以安全地进行程序化滚动。
This is what I ended up doing - feels hacky, but it works. 这就是我最终要做的-感觉很笨拙,但是可以。 If anyone has cleaner ways to do it, I'm happy to hear it.
如果有人有更清洁的方法来做,我很高兴听到。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// Don't change calendar month if currently scrolling
if (scrolling)
return;
// change calendar month
}
- (void)scrollEventsToCurrentMonth {
// find index path
// scroll to index path
scrolling = TRUE;
[self performSelector:@selector(stoppedScrolling) withObject:NULL afterDelay:0.5];
}
- (void)stoppedScrolling {
scrolling = FALSE;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.