簡體   English   中英

ios - UICollectionView在水平分頁時更改默認偏移量

[英]ios - UICollectionView change default offset when horizontal paging

我正在嘗試使用UICollectionView來顯示屏幕中心的當前視圖左側和右側的視圖。

我有正確顯示但水平分頁不以后續視圖為中心,因為它默認為幀的寬度,即320.0。

UICollectionView在哪里計算剪切到下一頁時使用的默認偏移值?

我想改變這個值。 有一個更好的方法嗎? 基本上我正在嘗試重新創建在iphone上的應用商店的搜索結果中使用的體驗。

事實證明,我必須設置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];
}

這個答案有所幫助: 使用不同的頁面寬度分頁UIScrollView

我認為最好的解決方案是添加一個自定義的UICollectionViewFlowLayout子類並重寫

- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)contentOffset  
                                 withScrollingVelocity:(CGPoint)velocity

我在這里寫了一個例子: https//gist.github.com/mmick66/9812223

嘗試在viewDidLayoutSubviews中設置contentOffset

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    if (viewDidLayoutSubviewsForTheFirstTime) {
        _collectionView.contentOffset = CGPointMake(...);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM