gpt4 book ai didi

ios - UIScrollView 委托(delegate)方法 scrollViewWillEndDragging : not working?

转载 作者:可可西里 更新时间:2023-11-01 06:09:59 24 4
gpt4 key购买 nike

我正在尝试使用 scrollViewWillEndDragging: 方法来滚动我自己的分页 UICollectionView,在两侧都有预览,类似于 App Store 应用程序。但是,如果我停止无惯性拖动(即,只需抬起手指),使用 ScrollView 下方的代码只会滚动到所需的矩形。如果我以任何惯性轻弹,该方法仍会被调用,但 ScrollView 不会滚动所需的矩形,它只是继续滚动?

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
withVelocity:(CGPoint)velocity
targetContentOffset:(inout CGPoint *)targetContentOffset
{
int itemWidth = 280;

MyCollectionView *collectionView = (MyIndexedCollectionView *) scrollView;

int totalPages = [self.colorArray[collectionView.index] count];
int currentPage;
int nextPage;

if (lastContentOffset < (int)scrollView.contentOffset.x)
{
// moved right
NSLog(@"scrolling right");
double currentPageOffset = ceil((scrollView.contentSize.width -
scrollView.contentOffset.x) / itemWidth);
currentPage = totalPages - currentPageOffset;
nextPage = currentPage >= totalPages ? totalPages : currentPage + 1;
}
else if (lastContentOffset > (int)scrollView.contentOffset.x)
{
// moved left
NSLog(@"scrolling left");
double currentPageOffset = floor((scrollView.contentSize.width -
scrollView.contentOffset.x) / itemWidth);
currentPage = totalPages - currentPageOffset;
nextPage = currentPage <= 0 ? 0 : currentPage - 1;
}

int xOffset = (nextPage * itemWidth);
int nextOffsetPage = (totalPages - ((scrollView.contentSize.width - xOffset) /
itemWidth)) + 1;

[scrollView scrollRectToVisible:CGRectMake(xOffset,
0,
collectionView.bounds.size.width,
collectionView.bounds.size.height)
animated:YES];
}

在放弃这种方法后,我尝试使用 scrollViewWillBeginDecelerating: 方法来代替,完全相同的代码可以完美地工作?我希望使用 scrollViewWillEndDragging: 方法的原因有两个:

  • 我想根据滚动速度调整它跳转到的项目
  • scrollViewWillBeginDecelerating: 方法不会如果您在没有任何惯性的情况下抬起手指停止拖动,则不会被调用。

任何想法,我是否误解了这个回调在做什么?

最佳答案

卫生部!!结果我应该一直使用 targetContentOffset 来设置我想要滚动到的偏移量而不是 scrollToRectToVisible: ala:

*targetContentOffset = CGPointMake(myTargetOffset, targetContentOffset->y);

RTFM:)

关于ios - UIScrollView 委托(delegate)方法 scrollViewWillEndDragging : not working?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19238474/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com