gpt4 book ai didi

ipad - UICollectionView 在覆盖 scrollViewWillEndDragging 时并不总是动画减速

转载 作者:行者123 更新时间:2023-12-04 14:25:35 26 4
gpt4 key购买 nike

我正在为我的 UICollectionView 创建自定义分页。我希望底部的一些单元格卡在屏幕边缘,但是,通过常规分页,滚动到下一页意味着如果页面底部的一半单元格显示,它只会显示另一半在下一页。我想让单元格卡在最后,但停止分页,以便卡在屏幕上的单元格清晰可见。

所以,为了做到这一点,我覆盖了函数 - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset

如果我拖动一两秒钟,它会按预期工作,但是,我试图模拟在启用分页时效果很好的“轻弹”。当我轻弹 UICollectionView 时,它会跳转到 targetContentOffset,而不是为其设置动画。

我如何防止这种情况?

这是我的代码:

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {

if(targetContentOffset->y < 400) {
targetContentOffset->y = 0;
return;
}

int baseCheck = 400;

while(baseCheck <= 10000) {
if(targetContentOffset->y > baseCheck && targetContentOffset->y < baseCheck + 800) {
targetContentOffset->y = (baseCheck + 340);
return;
}
baseCheck += 800;
}

targetContentOffset->y = 0;
}

最佳答案

我遇到了同样的问题并设法解决了它。就我而言,我正在模拟一个分页的 scrollView(实际上是一个 UICollectionView),其中页面大小小于 collectionView 本身的大小。我注意到滚动的“跳跃”发生在一组特定条件下:

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
CGFloat pageWidth; // defined somewhere else
NSInteger numberOfPages; // depends on your dataSource
CGFloat proposedOffset = targetContentOffset->x;
NSInteger currentPage = roundf(self.collectionView.contentOffset.x / pageWidth);
NSInteger proposedPage = roundf(proposedOffset / pageWidth);
// what follows is a fix for a weird case where the scroll 'jumps' into place with no animation
if(currentPage == proposedPage) {
if((currentPage == 0 && velocity.x > 0) ||
(currentPage == (numberOfPages - 1) && velocity.x < 0) ||
(currentPage > 0 && currentPage < (numberOfPages - 1) && fabs(velocity.x) > 0)
) {
// this forces the scrolling animation to stop in its current place
[self.collectionView setContentOffset:self.collectionView.contentOffset animated:NO];
[UIView animateWithDuration:.3
delay:0.
options:UIViewAnimationOptionCurveEaseOut
animations:^{
[self.collectionView setContentOffset:CGPointMake(currentPage * pageWidth, 0)];
}
completion:NULL];
}
}
targetContentOffset->x = (pageWidth * proposedPage);
}

关于ipad - UICollectionView 在覆盖 scrollViewWillEndDragging 时并不总是动画减速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15233845/

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