ios - UICollectionView在水平分页时更改默认偏移量

whi*_*uli 7 iphone ios ios6 uicollectionview

我正在尝试使用UICollectionView来显示屏幕中心的当前视图左侧和右侧的视图.

我有正确显示但水平分页不以后续视图为中心,因为它默认为帧的宽度,即320.0.

UICollectionView在哪里计算剪切到下一页时使用的默认偏移值?

我想改变这个值.有一个更好的方法吗?基本上我正在尝试重新创建在iphone上的应用商店的搜索结果中使用的体验.

whi*_*uli 11

事实证明,我必须设置pagingEnabled = NO并覆盖(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView以下内容:

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
  if (self.lastQuestionOffset > scrollView.contentOffset.x)
    self.currentPage = MAX(self.currentPage - 1, 0);
  else if (self.lastQuestionOffset < scrollView.contentOffset.x)
    self.currentPage = MIN(self.currentPage + 1, 2);

  float questionOffset = 290.0 * self.currentPage;
  self.lastQuestionOffset = questionOffset;
  [self.collectionView setContentOffset:CGPointMake(questionOffset, 0) animated:YES];
}
Run Code Online (Sandbox Code Playgroud)

这个答案有所帮助: 使用不同的页面宽度分页UIScrollView

  • 我发现的另一件事是在滚动时使用它:`[self.collectionView scrollRectToVisible:CGRectMake(questionOffset,0,self.collectionView.bounds.size.width,self.collectionView.bounds.size.height)animated:YES]; `它比上面使用的setContentOffset动画方法更顺畅. (2认同)

Mik*_*e M 6

我认为最好的解决方案是添加一个自定义的UICollectionViewFlowLayout子类并重写

- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)contentOffset  
                                 withScrollingVelocity:(CGPoint)velocity
Run Code Online (Sandbox Code Playgroud)

我在这里写了一个例子:https://gist.github.com/mmick66/9812223