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

}

相關文章
相關標籤/搜索