如何正确增加集合视图单元格之间的间距?

时间:2019-03-09 21:27:31

标签: ios swift uicollectionview uicollectionviewcell

我目前正在尝试在收藏视图单元格之间设置间距。问题是每次我水平滚动时,单元格都不会重置回应有的位置。我将此库用于集合视图单元格动画:https://github.com/KelvinJin/AnimatedCollectionViewLayout

请看下面的图片!

这是我的代码:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    // Turn on the paging mode for auto snaping support.
    collectionView?.isPagingEnabled = true

    if let layout = collectionView?.collectionViewLayout as? AnimatedCollectionViewLayout {
        layout.scrollDirection = direction
        layout.animator = animator?.0
    }

    dismissGesture.direction = direction == .horizontal ? .down : .left
}

extension ImageCollectionViewController: UICollectionViewDelegateFlowLayout {
override func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return Int(Int16.max)
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let c = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath)

    if let cell = c as? SimpleCollectionViewCell {
        let i = indexPath.row % vcs.count
        let v = vcs[i]
        cell.bind(color: v.0, imageName: v.1)
        cell.clipsToBounds = animator?.1 ?? true
    }

    return c
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    guard let animator = animator else { return view.bounds.size }
    return CGSize(width: view.bounds.width / CGFloat(animator.2), height: view.bounds.height / CGFloat(animator.3))
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return .zero
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 10
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

}

这里还有显示发生了什么的图像:

enter image description here

0 个答案:

没有答案