【问题标题】:Keep Collection View Cells Overlapped When They Are Redrawn重绘时保持集合视图单元格重叠
【发布时间】:2019-04-05 04:35:05
【问题描述】:

通过将集合视图布局的minimumLineSpacing 属性设置为负数,我已经让我的单元格重叠。但是,当我滚动并重新绘制单元格时,它们现在以相反的方向重叠。我在下面放了图片。

当集合视图滚动并重绘单元格时,如何保持单元格重叠,如第一张图片所示?

import UIKit

class PopularView: UIView {

let cellID = "cellID"

// MARK: - Views
let collectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .vertical
    layout.minimumLineSpacing = -55     // -------Allows Overlap-----
    layout.itemSize = CGSize(width: SCREEN_WIDTH, height: 185)
    layout.minimumInteritemSpacing = 17
    let view = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
    view.backgroundColor = .white
    return view
}()



// MARK: - Initializers
override init(frame: CGRect) {
    super.init(frame: frame)
    collectionView.dataSource = self
    collectionView.register(PopularCell.self, forCellWithReuseIdentifier: cellID)

    backgroundColor = .white
    setupCollectionView()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}



// MARK: - Setup
fileprivate func setupCollectionView() {
    self.addSubview(collectionView)
    collectionView.anchors(top: self.topAnchor, topPad: 0, bottom: self.bottomAnchor, bottomPad: 0, left: self.leftAnchor, leftPad: 0, right: self.rightAnchor, rightPad: 0, height: nil, width: nil)
    collectionView.contentSize = CGSize(width: 700, height: 700)
    }
}

extension PopularView: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 500
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! PopularCell
    cell.background.backgroundColor = .random
    return cell
    }
}

【问题讨论】:

  • 子类化 UICollectionViewLayout 并为每个项目使用自定义布局属性。最小间距可能无济于事。
  • 子类化UICollectionViewLayout时我会编辑哪些特定属性?
  • 查看this链接,看看它是否满足您的要求。
  • 绝对有帮助,但我仍然无法理解子类化布局属性的工作原理以及如何正确进行。

标签: ios swift uicollectionview uicollectionviewcell uicollectionviewlayout


【解决方案1】:

试试这个,可能对你有帮助:

1- 在你的单元格内,你可以在你的单元格内定义 innerView 并将框架设置为

let innerView:UIView = CGRect(x: 0,y: -overlapHeight,width: screenWidth, height:cell.height + overlapHeight)
cell?.contentView.addSubview(innerView)

2- 在初始化期间配置您的单元格:

cell?.contentView.clipsToBounds = false

3- 加载单元格时,设置z-order:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
        cell.layer.zPosition = CGFloat(indexPath.row)
// configure your cell after here
}

您应该能够看到内容视图中的嵌套视图有重叠。

我已经起草了一个示例代码,看起来并不完美,但可以帮助您入门:

private let reuseIdentifier = "Cell"
private let overlapHeight:CGFloat = 100
class CustomCollectionCell:UICollectionViewCell {
    var innerView:UIView?
    override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 300))
        self.backgroundColor = .darkGray
        let innerView = UIView(frame: CGRect(x: 0,y: -overlapHeight,width: UIScreen.main.bounds.width,height: overlapHeight + self.contentView.frame.height))
        self.innerView = innerView
        innerView.layer.cornerRadius = 20
        self.contentView.addSubview(innerView)
        self.contentView.clipsToBounds = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func configure(color:UIColor?) {
        innerView?.backgroundColor = color
    }
}

import UIKit

private let reuseIdentifier = "Cell"
private let overlapHeight:CGFloat = 100
class CustomCollectionCell:UICollectionViewCell {
    var innerView:UIView?
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .darkGray
        let innerView = UIView(frame: CGRect(x: 0,y: -overlapHeight,width: UIScreen.main.bounds.width,height: overlapHeight + self.contentView.frame.height))
        self.innerView = innerView
        innerView.layer.cornerRadius = 20
        self.contentView.addSubview(innerView)
        self.contentView.clipsToBounds = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func configure(color:UIColor?) {
        innerView?.backgroundColor = color
    }
}

class CollectionViewController: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = CGSize(width: UIScreen.main.bounds.width, height: 190)
        flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
        flowLayout.scrollDirection = .vertical
        flowLayout.minimumInteritemSpacing = 0.0
        collectionView.collectionViewLayout = flowLayout


        // Register cell classes
        self.collectionView!.register(CustomCollectionCell.self, forCellWithReuseIdentifier: reuseIdentifier)


    }

    // MARK: UICollectionViewDataSource

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return 30
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
        cell.layer.zPosition = CGFloat(indexPath.row)

        var color:UIColor?
        switch indexPath.row % 4 {
        case 0:
            color = .purple
        case 1:
            color = .yellow
        case 2:
            color = .green
        default:
            color = .red
        }
        if let cell = cell as? CustomCollectionCell {
            cell.configure(color: color)
        }


        return cell
    }



}

结果:

【讨论】:

  • 我在哪里可以访问innerView 属性?我似乎找不到如何使用我的 UICollectionViewCell 子类访问它。
  • innerView 是一个可以定义为 CollectionViewCell 的 firstChild 的视图。
  • 哦,我明白了。不幸的是,这并没有解决问题。
  • 嗯,您的代码中的某些内容看起来不正确
  • 你说什么需要改变?
猜你喜欢
  • 1970-01-01
  • 2018-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 2020-03-04
相关资源
最近更新 更多