滑动时将UICollectionViewCell与UIEdgeInsets居中

时间:2019-04-02 14:17:45

标签: swift uicollectionview uicollectionviewlayout swiper

我正在尝试创建需要根据图片居中的滑动组件,因此我设置了layout.sectionInset = UIEdgeInsets (top: 0, left: 32, bottom: 0, right: 32)

当我使用自动滚动调用collection.scrollToItem (at: indexPath, at: .centeredHorizontally, animated: true)时,它可以正常工作。但是,当我执行手动滑动时,无法集中。我无法更改pointee使其居中显示在屏幕上。

使用scrollToItem .centeredHorizontally

Auto Scroll

 let cellWithIncludingSpacing = (collection.layout.itemSize.width + collection.layout.minimumLineSpacing)

        var offset = targetContentOffset.pointee
        let index  = (offset.x + scrollView.contentInset.left) / cellWithIncludingSpacing
        let roundedIndex = round(index)

        offset = CGPoint(x: roundedIndex * cellWithIncludingSpacing - scrollView.contentInset.left,
                         y: -scrollView.contentInset.top)

        targetContentOffset.pointee = offset

我目前有以下结果:

Imgur

Link repo project

0 个答案:

没有答案