1.在使用UICollectionView 来显示横向滑动图片的时候,cell与cell之间有间隙,每次滑动后cell都会向左偏移,在使用过这两个方法才解决每次向左偏移的部分。
2.使用方法前不要开启分页效果,不然没有效果。
collectionView.pagingEnabled = NO;
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(0, 40, 0, 30);//第一个cell居中的效果,调用一次 上 左 下 右 的偏移量
}
- (void) scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
CGFloat offSetX = targetContentOffset->x; //偏移量
CGFloat itemWidth = kWidth-100; //itemSizem 的宽
//itemSizem的宽度+行间距 = 页码的宽度
NSInteger pageWidth = itemWidth + 30;
//根据偏移量计算 第几页
NSInteger pageNum = (offSetX+pageWidth/2)/pageWidth;
//根据显示的第几页,从而改变偏移量
targetContentOffset->x = pageNum*pageWidth;
NSLog(@"%.1f",targetContentOffset->x);
}