View with continuous scroll; both horizontal and vertical

后端 未结 4 547
迷失自我
迷失自我 2020-11-28 03:02

I have been struggling with this assignment for quite some time now. What I would like to develop is a scrollview or collectionview which scrolls continuously both vertical

4条回答
  •  爱一瞬间的悲伤
    2020-11-28 03:54

    @updated for swift 3 and changed how the maxRow is calculated otherwise the last column is cutoff and can cause errors

    import UIKit
    
    class NodeMap : UICollectionViewController {
        var rows = 10
        var cols = 10
    
        override func viewDidLoad(){
            self.collectionView!.collectionViewLayout = NodeLayout(itemWidth: 400.0, itemHeight: 300.0, space: 5.0)
        }
    
        override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return rows
        }
    
        override func numberOfSections(in collectionView: UICollectionView) -> Int {
            return cols
        }
    
        override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            return collectionView.dequeueReusableCell(withReuseIdentifier: "node", for: indexPath)
        }
    }
    
    class NodeLayout : UICollectionViewFlowLayout {
        var itemWidth : CGFloat
        var itemHeight : CGFloat
        var space : CGFloat
        var columns: Int{
            return self.collectionView!.numberOfItems(inSection: 0)
        }
        var rows: Int{
            return self.collectionView!.numberOfSections
        }
    
        init(itemWidth: CGFloat, itemHeight: CGFloat, space: CGFloat) {
            self.itemWidth = itemWidth
            self.itemHeight = itemHeight
            self.space = space
            super.init()
        }
    
        required init(coder aDecoder: NSCoder) {
            self.itemWidth = 50
            self.itemHeight = 50
            self.space = 3
            super.init()
        }
    
        override var collectionViewContentSize: CGSize{
            let w : CGFloat = CGFloat(columns) * (itemWidth + space)
            let h : CGFloat = CGFloat(rows) * (itemHeight + space)
            return CGSize(width: w, height: h)
        }
    
        override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
            let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
            let x : CGFloat = CGFloat(indexPath.row) * (itemWidth + space)
            let y : CGFloat = CGFloat(indexPath.section) + CGFloat(indexPath.section) * (itemHeight + space)
            attributes.frame = CGRect(x: x, y: y, width: itemWidth, height: itemHeight)
            return attributes
        }
    
        override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
            let minRow : Int = (rect.origin.x > 0) ? Int(floor(rect.origin.x/(itemWidth + space))) : 0
            let maxRow : Int = min(columns - 1, Int(ceil(rect.size.width / (itemWidth + space)) + CGFloat(minRow)))
            var attributes : Array = [UICollectionViewLayoutAttributes]()
            for i in 0 ..< rows {
                for j in minRow ... maxRow {
                    attributes.append(self.layoutAttributesForItem(at: IndexPath(item: j, section: i))!)
                }
            }
            return attributes
        }
    }
    

提交回复
热议问题