UICollectionView中的渐变单元格

时间:2018-07-20 18:38:04

标签: ios swift uicollectionview

是否可以在UICollectionView中制作渐变单元?我在ViewController文件中尝试了此操作,但没有任何反应:

extension ViewController: UICollectionViewDataSource, UICollectionViewDelegate {

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

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

        if let itemCell = collectionView.dequeueReusableCell(withReuseIdentifier: "main_menu_cell", for: indexPath) as? MainWindowCollectionViewCell {

            itemCell.menu = itemMenuArray[indexPath.row]

            let gradientLayer = CAGradientLayer()
            gradientLayer.colors = [UIColor(red: 245/255, green: 78/255, blue: 162/255, alpha: 1.0), UIColor(red: 255/255, green: 118/255, blue: 118/255, alpha: 1.0)]
            gradientLayer.startPoint = CGPoint(x: 0, y: 0)
            gradientLayer.endPoint = CGPoint(x: 1.0, y: 1.0)
            gradientLayer.frame = view.bounds

            itemCell.layer.addSublayer(gradientLayer)

            return itemCell
        }

        return UICollectionViewCell()
    }
}

0 个答案:

没有答案