UICollectionView 圖片橫向滑動居中偏移量的解決


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);

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM