use*_*207 5 ios uicollectionview
我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;
}
Run Code Online (Sandbox Code Playgroud)
我pageControl用来表示我有多少页面和当前页面.
我使用类似于以下链接的方法来确定当前页面.
iOS6 UICollectionView和UIPageControl - 如何获取可见单元格?
如下.
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSInteger currentPage = self.collectionView.contentOffset.x / self.collectionView.frame.size.width;
self.pageControl.currentPage = currentPage;
}
Run Code Online (Sandbox Code Playgroud)
但是,它不起作用.当我滑动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));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3661 次 |
| 最近记录: |