UICollectionView 图片横向滑动居中偏移量的解决

1.在使用UICollectionView 来显示横向滑动图片的时候,cell与cell之间有间隙,每次滑动后cell都会向左偏移,在使用过这两个方法才解决每次向左偏移的部分。spa

2.使用方法前不要开启分页效果,否则没有效果。图片

 collectionView.pagingEnabled = NO;ci

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{get

    return UIEdgeInsetsMake(0, 40, 0, 30);//第一个cell居中的效果,调用一次   上 左 下 右 的偏移量it

    }io

 

- (void) scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {class

    CGFloat offSetX = targetContentOffset->x; //偏移量分页

    CGFloat itemWidth = kWidth-100;   //itemSizem 的宽scroll

    //itemSizem的宽度+行间距 = 页码的宽度方法

    NSInteger pageWidth = itemWidth + 30;     

    //根据偏移量计算 第几页

    NSInteger pageNum = (offSetX+pageWidth/2)/pageWidth;

    //根据显示的第几页,从而改变偏移量

    targetContentOffset->x = pageNum*pageWidth;

    NSLog(@"%.1f",targetContentOffset->x);

}

相关文章
相关标签/搜索