Page count of UICollectionView with paging in iOS

拈花ヽ惹草 提交于 2019-11-29 04:12:23

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

If the UICollectionView scrolls horizontally, you divide its contentSize's width to its frame's width:

int pages = floor(self.collectionView.contentSize.width /    
                  self.collectionView.frame.size.width) + 1;

If it scrolls vertically, you divide its contentSize's height to its frame's height:

int pages = floor(self.collectionView.contentSize.height /    
                  self.collectionView.frame.size.height) + 1;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!