代码之家  ›  专栏  ›  技术社区  ›  Nikunj Kumbhani

滚动后UITableViewCell内UIView中的渐变

  •  0
  • Nikunj Kumbhani  · 技术社区  · 6 年前

    我已将渐变色设置为 UIView UITableViewCell . 在第一次加载时一切正常,但是在滚动之后,渐变颜色再次加载,并且每次都比实际设置条件改变位置。下面是我实现渐变色的代码:

    我该怎么办?

    添加渐变层

    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
    }
    

    等级

    class AllOfferlistCell: UITableViewCell {
    
        @IBOutlet weak var lbltitle: UILabel!
        @IBOutlet weak var lblPopular: UILabel!
        @IBOutlet weak var VwPercentage: UIView!   
    }
    

    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

    2 回复  |  直到 6 年前
        1
  •  2
  •   SPatel mash    6 年前

    创建自定义视图,如下所示

    @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. 改变 VW百分比 三角形半径视图 .

      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
      

      }

        2
  •  0
  •   Nilesh Solanki    6 年前

    因为当您滚动UITableView时,单元格被重用,所以每个重用的单元格的渐变色都会发生变化。

    初始化渐变颜色数组

    let primaryGradientColorsArray = [Color1,Color2,Color3];
    let secondaryGradientColorsArray = [Color1,Color2,Color3];
    

    在UITableView中

    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: [primaryGradientColorsArray[indexPath.row],secondaryGradientColorsArray[indexPath.row]]), at: 1)
        cell.lblPopular.text = "POPULAR"
    
    }else{
    
        cell.VwPercentage.layer.insertSublayer(gradient(frame: cell.VwPercentage.bounds, colors: [primaryGradientColorsArray[indexPath.row],secondaryGradientColorsArray[indexPath.row]]), at: 1)
        cell.lblPopular.text = "50% OFF"
    }
    
    return cell
    }