UIcollectionView Cell设置ZIndex。我想使用UICollectionView创建Card Cell

时间:2018-05-14 13:33:54

标签: swift uicollectionview cell

夫特 我正在尝试创建卡式单元格。所以我使用集合视图并将minimumLineSpacingForSectionAt设置为否定此代码正常工作它看起来不错。

问题是当我有一个从下到上的卷轴时,第一个单元格是顶部,但我没有需要在这里我已经附加了我的代码,请检查。滚动问题

后,我附加了错误图片
class SecondViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {

    let kItemSpace: CGFloat = -60.0

     @IBOutlet weak var collection_setting: UICollectionView!
     @IBOutlet var btn_top: UIButton!

     var ary: NSMutableArray = NSMutableArray()

      var ary_show: NSMutableArray = NSMutableArray()

    override func viewDidLoad() {
        super.viewDidLoad()
          ary = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"]

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    //-----------------------------------------------------------------------------------
    // MARK: - collection DataSource & Delegate
    //-----------------------------------------------------------------------------------



    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return self.ary.count



    }

    // make a cell for each cell index path
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "smallCollectionViewCell", for: indexPath as IndexPath) as! smallCollectionViewCell

            cell.inner_view.dropShadow()
            cell.inner_view.backgroundColor = .random()
           cell.layer.zPosition = -1
            cell.lbl_.text = "DEMO - \(indexPath.row)"
        //cell.bringSubview(toFront: collectionView)
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

            return CGSize(width: collectionView.frame.size.width, height: 100 )



    }
    public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
    {



    }



    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: NSInteger) -> CGFloat {
        return kItemSpace
    }


    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        print(indexPath.row)

        if(indexPath.row == 14)
        {
            print("--------------->",indexPath.row)
            cell.layer.zPosition = 0
        }else
        {
            cell.layer.zPosition = -1
        }

    }







}

enter image description here

Demo - 7,Demo - 8,Demo - 9看起来不错,但其他人并没有按照我的意愿展示。这是第一次看起来正确。滚动后出现此问题。

2 个答案:

答案 0 :(得分:0)

请试试此代码。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "smallCollectionViewCell", for: indexPath as IndexPath) as! smallCollectionViewCell
    if cell == nil {
        cell.inner_view.dropShadow()
        cell.inner_view.backgroundColor = .random()
     }
       cell.layer.zPosition = -1
        cell.lbl_.text = "DEMO - \(indexPath.row)"
    return cell
}

答案 1 :(得分:0)

cell.layer.zPosition = CGFloat(array.count-indexPath.row)