View with continuous scroll; both horizontal and vertical

后端 未结 4 553
迷失自我
迷失自我 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:43

    @rdelmar's answer worked like a charm, but I needed to do it in swift. Here's the conversion :)

    class NodeMap : UICollectionViewController {
        @IBOutlet var activateNodeButton : UIBarButtonItem?
        var rows = 10
        var cols = 10
        override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return rows
        }
        override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
            return cols
        }
        override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
            return collectionView.dequeueReusableCellWithReuseIdentifier("node", forIndexPath: indexPath)
        }
        override func viewDidLoad() {
            self.collectionView!.collectionViewLayout = NodeLayout(itemWidth: 100.0, itemHeight: 100.0, space: 5.0)
        }
    }
    
    class NodeLayout : UICollectionViewFlowLayout {
        var itemWidth : CGFloat
        var itemHeight : CGFloat
        var space : CGFloat
        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 func collectionViewContentSize() -> CGSize {
            let w : CGFloat = CGFloat(self.collectionView!.numberOfItemsInSection(0)) * (itemWidth + space)
            let h : CGFloat = CGFloat(self.collectionView!.numberOfSections()) * (itemHeight + space)
            return CGSizeMake(w, h)
        }
        override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes! {
            let attributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
            let x : CGFloat = CGFloat(indexPath.row) * (itemWidth + space)
            let y : CGFloat = CGFloat(indexPath.section) + CGFloat(indexPath.section) * (itemHeight + space)
            attributes.frame = CGRectMake(x, y, itemWidth, itemHeight)
            return attributes
        }
        override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {
            let minRow : Int = (rect.origin.x > 0) ? Int(floor(rect.origin.x/(itemWidth + space))) : 0
            let maxRow : Int = Int(floor(rect.size.width/(itemWidth + space)) + CGFloat(minRow))
            var attributes : Array = [UICollectionViewLayoutAttributes]()
            for i in 0...self.collectionView!.numberOfSections()-1 {
                for j in minRow...maxRow {
                    attributes.append(self.layoutAttributesForItemAtIndexPath(NSIndexPath(forItem: j, inSection: i)))
                }
            }
            return attributes
        }
    }
    

提交回复
热议问题