ios - 根据单元格索引路径向 TableView 单元格元素添加渐变层

标签 ios swift uitableview gradient

我在将渐变图层作为背景添加到表格 View 单元格内的元素时遇到困难。渐变层取决于单元格索引路径。我有一个颜色的 enum,它使用 indexPath 作为它的 rawValue。所需的输出很简单,例如 this

这是创建渐变层的函数。我用了这个tutorial编写函数。

func setGradientBackground(colorOne: UIColor, colorTwo: UIColor, name: String) {
    let gradient = CAGradientLayer()
    gradient.frame = bounds
    gradient.colors = [colorOne.cgColor, colorTwo.cgColor]
    gradient.locations = [0, 1]
    gradient.startPoint = CGPoint(x: 1, y: 1)
    gradient.endPoint = CGPoint(x: 0, y: 0)
    gradient.name = name
    layer.insertSublayer(gradient, at: 0)
}

要将渐变添加到表格 View 单元格中的元素,已经尝试了几件事。目前我正在遍历可见单元格以添加图层。我试过从 viewWillLayoutSubviews 调用。这是我唯一一次看到应用的渐变。

我看到了两个问题。该应用程序变得无法使用,因为我认为当 View 上的某些内容发生变化时,此功能或多或少会被连续调用。并且图层绘制在单元格中的所有其他内容之上。

func configTableCells() {
    guard let cells = self.tableView.visibleCells as? [GroupTableCell] else {return}
    for (i, cell) in cells.enumerated() {
        if shouldAddSubLayer(cell) {
            guard let colorOne = Colors(rawValue: i)?.classicColor,
                let colorTwo = Colors(rawValue: i)?.alternativeColor else {continue}
            cell.taskInfoCollectionView.setGradientBackground(colorOne: colorOne,
                                                              colorTwo: colorTwo,
                                                              name: cellLayerName)
        }
    }
}

我尝试在 cellForRowAtwillDisplay Cell 中添加层,但这没有用。我相信这是因为细胞还不“存在”,所以添加一层没有区别。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: groupCellId, for: indexPath) as! GroupTableCell
    cell.groupNameLabel.text = "Group Name"
    cell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
    if shouldAddSubLayer(cell) {
        if let colorOne = Colors(rawValue: indexPath.row)?.classicColor,
            let colorTwo = Colors(rawValue: indexPath.row)?.alternativeColor {
        cell.taskInfoCollectionView.setGradientBackground(colorOne: colorOne,
                                                          colorTwo: colorTwo,
                                                          name: cellLayerName)
        }
    }
    return cell
} 

我还包含了这个函数,因为我阅读了另一个 stackOverflow 问题,我应该检查单元格是否已经有层以避免连续添加它。 (我无法链接问题,因为我现在找不到它)

func shouldAddSubLayer(_ cell: GroupTableCell) -> Bool {
    guard let sublayers = cell.layer.sublayers else {return true}
    return sublayers.filter({ $0.name == cellLayerName }).count == 0
}

欢迎就如何实现此工作提出任何建议,谢谢。

最佳答案

试试VisualEffect

在您的 ViewDidLoad() 中添加以下代码

if (!UIAccessibilityIsReduceTransparencyEnabled()) {
tableView.backgroundColor = UIColor.clear
let blurEffect = UIBlurEffect(style: .light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
tableView.backgroundView = blurEffectView

//if inside a popover
if let popover = navigationController?.popoverPresentationController {
    popover.backgroundColor = UIColor.clear
}

//if you want translucent vibrant table view separator lines
tableView.separatorEffect = UIVibrancyEffect(blurEffect: blurEffect)}

关于ios - 根据单元格索引路径向 TableView 单元格元素添加渐变层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58166777/

相关文章:

Swift 如何在所有 uiTextfield 上退出第一响应者

ios - 从 uitableview cellForRowAtIndexPath 委托(delegate)中删除行时应用程序崩溃

ios - 使用 AVAssetReaderVideoCompositionOutput 导出进行到一半,然后以 AVFoundationErrorDomain Code=-11800 退出

ios - xcode 8.2 IPA 验证警告

ios - 在后台运行应用程序并播放一些音频和加速度计

ios - 从顶部(如下拉菜单)隐藏的动画 TableView

ios - Swift - 如何将触摸事件传递到属于第二个 UIWindow 的多个按钮,但忽略其他所有内容

ios - 从推送通知打开应用程序时打开特定的 View Controller

ios - 使用自动布局动画化 UIView 动画化 UITableView 的复选标记

ios - 具有不同 cell.accessoryType 的不同部分