ios - 滚动后 UITableViewCell 中的 UIView 中的渐变发生变化

标签 ios swift uitableview cagradientlayer

我在 UITableViewCell 中为 UIView 设置了渐变颜色。第一次加载时一切正常,但滚动后,渐变颜色再次加载,每次都比实际设置条件更改位置。这是我实现渐变颜色的代码:

我该怎么办?

添加渐变图层

func gradient(frame:CGRect,colors: [CGColor]) -> CAGradientLayer {
    let layer = CAGradientLayer()
    layer.frame = frame
    layer.startPoint = CGPoint(x: 1.0, y: 0.0)
    layer.endPoint = CGPoint(x: 0.0, y: 1.0)
    layer.locations = [0.5,0.35]
    layer.colors = colors
    return layer
}

UITableViewCell

class AllOfferlistCell: UITableViewCell {

    @IBOutlet weak var lbltitle: UILabel!
    @IBOutlet weak var lblPopular: UILabel!
    @IBOutlet weak var VwPercentage: UIView!   
}

tableView cellForRowAtindexPath

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! AllOfferlistCell

    cell.lbltitle.text = "Index \(indexPath.row)"

    if self.DataArray[indexPath.row].Flag == "1"{

        cell.VwPercentage.layer.insertSublayer(gradient(frame: cell.VwPercentage.bounds, colors: [UIColor.red.cgColor,UIColor.white.cgColor]), at: 1)
        cell.lblPopular.text = "POPULAR"

    }else{

        cell.VwPercentage.layer.insertSublayer(gradient(frame: cell.VwPercentage.bounds, colors: [UIColor.blue.cgColor,UIColor.white.cgColor]), at: 1)
        cell.lblPopular.text = "50% OFF"
    }

    return cell
}

输出:

第一次加载

enter image description here

滚动后

enter image description here

最佳答案

像下面这样创建自定义 View

@IBDesignable class TriangleGradientView: UIView {
    @IBInspectable var topColor: UIColor = UIColor.red {
        didSet {
            setGradient()
        }
    }
    @IBInspectable var bottomColor: UIColor = UIColor.blue {
        didSet {
            setGradient()
        }
    }
    
    override class var layerClass: AnyClass {
        return CAGradientLayer.self
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setGradient()
    }
    
    private func setGradient() {
        (layer as! CAGradientLayer).colors = [topColor.cgColor, bottomColor.cgColor]
        (layer as! CAGradientLayer).startPoint = CGPoint(x: 1.0, y: 0.0)
        (layer as! CAGradientLayer).endPoint = CGPoint(x: 0.0, y: 1.0)
        (layer as! CAGradientLayer).locations = [0.5,0.35]
    }

}

使用

  1. 在界面生成器中将 VwPercentage 的自定义类设置为 TriangleGradientView

  2. VwPercentage 类型更改为 TriangleGradientView

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! AllOfferlistCell
        cell.lbltitle.text = "Index \(indexPath.row)"
        if self.DataArray[indexPath.row].Flag == "1"{
            cell.VwPercentage.topColor = .red
            cell.lblPopular.text = "POPULAR"
        }else{
            cell.VwPercentage.topColor = .blue
            cell.lblPopular.text = "50% OFF"
        }
        cell.VwPercentage.bottomColor = .white
        return cell
    }
    

关于ios - 滚动后 UITableViewCell 中的 UIView 中的渐变发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52602875/

相关文章:

ios - 我可以从UICollectionView中“取出”或“撕下”一个单元格吗?

ios - Jenkins 作为运行 IOS 模拟器的 OSX 上的服务用户

ios - 如何在 didFinishLaunchingWithOptions 方法中通过单击通用链接检查应用程序是否已打开

ios - 将子实体追加到核心数据 iOS Swift 中的实体

ios - 如何从 Settings.bundle 更改切换开关的默认值

ios - 为什么滑动手势识别器会 swift 出错?

iOS、通用链接、Swift。 continueUserActivity 不调用

ios - 字典中的单元格标题

iphone - UITableViewCell 背景颜色在单元格的左右边缘是不同的阴影

ios - 为什么 UITextField 作为自定义 UITableViewCell 的 subview 会抑制父 UITableView 的滚动?