簡體   English   中英

無法獲取UICollectionView當前頁面

[英]Could not get UICollectionView current page

我使用以下代碼在水平方向上對collection view進行了分頁。

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.collectionView.pagingEnabled = YES;
    self.collectionView.directionalLockEnabled = YES;
    self.collectionView.bounces = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.delegate = self;

    [self setup];

    self.pageControl.currentPage = 0;

    self.pageControl.numberOfPages = floor(self.collectionView.contentSize.width /   self.collectionView.frame.size.width) + 1;
}

我正在使用pageControl來指示我有多少頁面和當前頁面。

我使用類似於以下鏈接的方法來確定當前頁面。

iOS6 UICollectionView和UIPageControl - 如何獲取可見單元格?

UICollectionView:頁面控制的當前索引路徑

如下。

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger currentPage = self.collectionView.contentOffset.x / self.collectionView.frame.size.width;
    self.pageControl.currentPage = currentPage;
}

但是,它不起作用。 當我將collection view滑動到下一頁時, pageControl仍然指向第一頁。 它不會改變它的價值。

我打印出self.collectionView.contentOffset.x值,它總是在框架內(這是第一頁,即使我已經刷到下一頁),它永遠不會進入下一頁。

我做錯什么了嗎?

你在考慮細胞之間的間距嗎?

嘗試這個:

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    float currentPage = self.collectionView.contentOffset.x / self.collectionView.frame.size.width;
    self.pageControl.currentPage = ceil(currentPage);

    NSLog(@"Values: %f %f %f", self.collectionView.contentOffset.x, self.collectionView.frame.size.width, ceil(currentIndex));
}

Swift 4.2頁面控件獲取集合視圖當前頁面

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

        let pageWidth = scrollView.frame.size.width
        currentPage = Int((scrollView.contentOffset.x + pageWidth / 2) / pageWidth)
        pageControl.currentPage = currentPage
    }

暫無
暫無

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

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