Swift 2集合视图扩展,用于水平滚动到垂直

时间:2016-08-11 08:22:02

标签: swift2 uicollectionview uicollectionviewlayout uiscrollviewdelegate

我的收藏夹视图有这个扩展,可以进行水平滚动,但我想在垂直滚动中更改它?

extension viewRe : UIScrollViewDelegate
{
    func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>)
    {
        let layout = self.recipesCollView?.collectionViewLayout as! UICollectionViewFlowLayout
        let cellWidthIncludingSpacing = layout.itemSize.width + layout.minimumLineSpacing

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

        offset = CGPoint(x: roundedIndex * cellWidthIncludingSpacing - scrollView.contentInset.left, y: -scrollView.contentInset.top)
        targetContentOffset.memory = offset
    }
}

通过这个扩展,即使他滚动因为启用了分页,我也试图让单元格坚持到视图的顶部。因此,每当用户滚动时,我希望单元格在用户滚动时粘在顶部等等。

1 个答案:

答案 0 :(得分:2)

@donnyWals是对的。如果您使用的是UICollectionView,只需更改其UICollectionViewFlowLayout

即可
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = . Horizontal
let collectionView = UICollectionView(frame: frame, collectionViewLayout: layout)

或者如果您有UICollectionView

if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
    layout.scrollDirection = .Horizontal
}

按照官方API:

UICollectionView

UICollectionViewFlowLayout