在集合中查看查看不正确集中
问题描述:
我有一个启用了分页的UICollectionView,并且水平移动的各个视图没有正确居中。 我确保视图与屏幕宽度相同。在集合中查看查看不正确集中
关于如何强制UICollectionView水平页面视图的任何指针?
答
您必须确保您的部分插入+行间距+单元格宽度全部等于CollectionView的边界。我用一个自定义UICollectionViewFlowLayout sublcass以下方法:
- (void)adjustSpacingForBounds:(CGRect)newBounds {
NSInteger count = newBounds.size.width/self.itemSize.width - 1;
CGFloat spacing = (newBounds.size.width - (self.itemSize.width * count))/count;
self.minimumLineSpacing = spacing;
UIEdgeInsets insets = self.sectionInset;
insets.left = spacing/2.0f;
self.sectionInset = insets;
}
你要记住,UICollectionView只是一个UIScrollView的sublcass所以它不知道你是怎么想的分页工作。在UICollectionView之前,您必须在布置UIScrollView的子视图时处理所有这些数学运算。
+0
我在哪里放置这种方法?何时/我在哪里打电话? – 2016-04-23 19:18:42
答
覆盖的方法:
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
*targetContentOffset = scrollView.contentOffset; // set acceleration to 0.0
float pageWidth = (float)self.articlesCollectionView.bounds.size.width;
int minSpace = 10;
int cellToSwipe = (scrollView.contentOffset.x)/(pageWidth + minSpace) + 0.5; // cell width + min spacing for lines
if (cellToSwipe < 0) {
cellToSwipe = 0;
} else if (cellToSwipe >= self.articles.count) {
cellToSwipe = self.articles.count - 1;
}
[self.articlesCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:cellToSwipe inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}
你解决这个问题?我有同样的问题。这些细胞偏离中心,因为我滚动偏心的化合物使其变得更糟。 – IkegawaTaro 2013-04-20 05:40:31
@IkegawaTaro同样的问题在这里。你有没有解决它? – 2016-04-23 19:20:02