Collectionview Как расширить определенную ячейку с помощью StickyCollectionViewFlowLayout - PullRequest
0 голосов
/ 02 февраля 2020

Интегрировано с StickyCollectionViewFlowlayout https://github.com/matbeich/StickyCollectionView-Swift

GIF-изображение Как развернуть ячейку с помощью StickyCollectionViewFlowlayout для конкретной ячейки, щелкните развернуть и раскрыть размер ячейки

Мне нужно, чтобы это расширилось и раскрылось.

Нужно изображение Нравится

Вот мой код

class StickyCollectionViewFlowLayout: UICollectionViewFlowLayout {

    var firstItemTransform: CGFloat?

    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        let items = NSArray (array: super.layoutAttributesForElements(in: rect)!, copyItems: true)
        var headerAttributes: UICollectionViewLayoutAttributes?

        items.enumerateObjects(using: { (object, idex, stop) -> Void in
            let attributes = object as! UICollectionViewLayoutAttributes

            if attributes.representedElementKind == UICollectionView.elementKindSectionHeader {
                headerAttributes = attributes
            }
            else {
                self.updateCellAttributes(attributes, headerAttributes: headerAttributes)
            }
        })
        return items as? [UICollectionViewLayoutAttributes]
    }

    func updateCellAttributes(_ attributes: UICollectionViewLayoutAttributes, headerAttributes: UICollectionViewLayoutAttributes?) {

        let minY = collectionView!.bounds.minY + collectionView!.contentInset.top
        var maxY = attributes.frame.origin.y

        if let headerAttributes = headerAttributes {
            maxY -= headerAttributes.bounds.height
        }

        let finalY = max(minY, maxY)
        var origin = attributes.frame.origin

        let deltaY = (finalY - origin.y) / attributes.frame.height

        if let itemTransform = firstItemTransform {
            let scale = 1 - deltaY * itemTransform
            attributes.transform = CGAffineTransform(scaleX: scale, y: scale)
        }

        origin.y = finalY
        attributes.frame = CGRect(origin: origin, size: attributes.frame.size)
        attributes.zIndex = attributes.indexPath.row
    }

    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        return true
    }
}
class CornerViewController: UIViewController {

    let kCellHeight: CGFloat = 120.0
    let kItemSpace: CGFloat = -20.0
    let kDemoCell = "cornerCell"

    let citiesArray = ["Bangkok", "Barcelona", "Beijing", "Istanbul", "Kiev", "Kyoto", "London", "Madrid", "Moscow", "New York", "Paris", "Prague", "Rio", "Rome", "St.Petersburg", "Tokyo", "Venice", "Vienna"]
    let colorsArray = ["EE5464", "DC4352", "FD6D50", "EA583F", "F6BC43", "8DC253", "4FC2E9", "3CAFDB", "5D9CEE", "4B89DD", "AD93EE", "977BDD", "EE87C0", "D971AE", "903FB1", "9D56B9", "227FBD", "2E97DE"]
    var indexPathRow: IndexPath?

    @IBAction func actionClose(_ sender: AnyObject) {
        dismiss(animated: true, completion: nil)
    }
}

extension CornerViewController: UICollectionViewDataSource {

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return citiesArray.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kDemoCell, for: indexPath) as! CornerCollectionViewCell

        let city: String = citiesArray[indexPath.row]
        let hexString = colorsArray[indexPath.row]
        let color = UIColor.colorFromHexString(hexString)

        cell.city = city
        cell.color = color
        return cell
    }
}

extension CornerViewController: UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPathRow == indexPath {
            //            if let layoutCell = collectionView.collectionViewLayout as? StickyCollectionViewFlowLayout {
            //                layoutCell.minimumLineSpacing = 300
            //            }
            //        /layout.minimumLineSpacing = 8
            return CGSize(width: view.bounds.width, height: kCellHeight+100)
        }else{
            return CGSize(width: view.bounds.width, height: kCellHeight)
        }
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: NSInteger) -> CGFloat {
        return kItemSpace
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print(indexPath.row)
        indexPathRow = indexPath
    let item = collectionView.cellForItem(at: indexPath) as! CornerCollectionViewCell
    UIView.animate(withDuration: 1.0, animations: {
        self.view.bringSubviewToFront(collectionView)
        collectionView.bringSubviewToFront(item)
        item.frame.origin = self.view.frame.origin   /// this view origin will be at the top of the scroll content, you'll have to figure this out
        item.frame.size.width = self.view.frame.width
        item.frame.size.height = self.view.frame.height/2
    })
    }
}

Пожалуйста, если вы знаете какие-либо предложения с развернуть ячейку и раскрыть с той же ячейкой нажал.

Спасибо.

...