Page count of UICollectionView with paging in iOS

后端 未结 2 1678
野性不改
野性不改 2020-12-16 20:47

Consider a UICollectionView with flow layout and paging enabled (by setting pagingEnabled to YES).

What would be the simplest

2条回答
  •  失恋的感觉
    2020-12-16 21:04

    The right answer should be:

    If the UICollectionView scrolls horizontally:

    int pages = ceil(self.collectionView.contentSize.width /    
                       self.collectionView.frame.size.width);
    

    If it scrolls vertically:

     int pages = ceil(self.collectionView.contentSize.height /    
                       self.collectionView.frame.size.height);
    

    follow to wiki:

    In mathematics and computer science, the floor and ceiling functions map a real number to the largest previous or the smallest following integer, respectively. More precisely, floor(x) is the largest integer not greater than x and ceiling(x) is the smallest integer not less than x.

    Here is my result to check:

    ceil(2.0/5.0) = 1.000000
    ceil(5.0/5.0) = 1.000000
    ceil(6.0/5.0) = 2.000000
    ceil(10.0/5.0) = 2.000000
    ceil(11.0/5.0) = 3.000000
    

提交回复
热议问题