使用渐变

时间:2018-01-20 16:17:08

标签: ios swift uitableview uiview

我在TableViewController上有一个自定义的TableViewCell。 单元格有一个UIView,它变成了一个渐变,底部显示黑色,UIView顶部显示透明(透明)颜色。

当应用程序加载时看起来很好,但是当我滚动时,渐变变得越来越黑,“渐弱”本身变得越来越小,越来越接近UIView的顶部。

我不知道如何让它保持最初应用程序加载时的状态。

这是我的代码:

let gradient = CAGradientLayer()
gradient.frame = cell.gradientView.bounds
let colour:UIColor = .black
gradient.colors = [colour.withAlphaComponent(0.0).cgColor,colour.cgColor]

cell.gradientView.layer.insertSublayer(gradient, at: 0)

1 个答案:

答案 0 :(得分:1)

在awakeFromNib中添加此代码而不是在cellForRow中,因为出列的渐变已经多次添加到单元格

   let gradient = CAGradientLayer()
    gradient.frame = cell.gradientView.bounds
    let colour:UIColor = .black
    gradient.colors = [colour.withAlphaComponent(0.0).cgColor,colour.cgColor]
    self.gradientView.layer.insertSublayer(gradient, at: 0)