简体   繁体   English

UICollectionView-分隔scrollViewDidScroll和scrollToItemAtIndexPath

[英]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 ,我使用scrollViewDidScrollUICollectionView 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.

相关问题 UICollectionView scrollToItemAtIndexPath - UICollectionView scrollToItemAtIndexPath 无休止的UICollectionView幻灯片+ scrollToItemAtIndexPath - Endless UICollectionView Slideshow + scrollToItemAtIndexPath UICollectionView水平滚动scrollToItemAtIndexPath之后: - UICollectionView Horizontal Scroll after scrollToItemAtIndexPath: UICollectionView.scrollToItemAtIndexPath如何工作? - How UICollectionView.scrollToItemAtIndexPath works? UICollectionView scrollToItemAtIndexPath不起作用 - UICollectionView scrollToItemAtIndexPath doesn't work 如果在viewWillAppear中调用scrollToItemAtIndexPath,则在UICollectionView上未更新contentOffset - contentOffset not updated on UICollectionView if scrollToItemAtIndexPath is called inside viewWillAppear 使用UICollectionView scrollToItemAtIndexPath返回到上一个选定的单元格 - Using UICollectionView scrollToItemAtIndexPath to return to last selected cell UIcollectionView scrollToitemAtIndexPath 不适用于不可见的单元格 - UIcollectionView scrollToitemAtIndexPath does not work for non visible cells 在scrollToItemAtIndexPath之后获取UICollectionView单元格位置 - Get UICollectionView cell position after scrollToItemAtIndexPath UICollectionView scrollToItemAtIndexPath在导航控制器中无法正常运行 - UICollectionView scrollToItemAtIndexPath not functioning properly in navigation controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM