繁体   English   中英

在UITableViewCell的背景图像上添加渐变

[英]Adding gradient on background image of UITableViewCell

我在UIViewController有一个UITableView 我正在尝试在UITableViewCell的背景图像上应用渐变。 我可以绘制渐变,但是它在颜色之间绘制了一条清晰的线,而不是渐变。

在我的cellForRowAt ,我将一个对象分配给CustomTableViewCell ,如下所示:

  let cellIdentifier = "Cell"
  // all the standard data source, delegate stuff for uitableviews

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! CustomTableViewCell
    let scheduleItem = scheduleData[indexPath.row]
    cell.scheduleStruct = scheduleItem
    return cell
  }

CustomTableViewCell ,我声明了以下属性:

    public var scheduleStruct: FullScheduleStruct? {
    didSet {
      configureCell()
    }
    // emptyView to throw on top of a background image
    var gradientView: UIView?

我还使用Kingfisher将图像从Internet提取出来,这就是ImageResource(downloadURL: url)部分的作用。 设置scheduleStruct时调用的configureCell()方法如下:

    private func configureCell() {

    // get the background image
    if let url = scheduleStruct?.show.image?.original {
      let imageUrl = ImageResource(downloadURL: url)
      backgroundImageView.contentMode = .scaleAspectFill
      backgroundImageView.kf.setImage(with: imageUrl)

      // configure the gradientView
      gradientView = UIView(frame: backgroundImageView.frame)
      let gradient = CAGradientLayer()
      gradient.frame = gradientView!.frame
      gradient.colors = [UIColor.clear.cgColor, UIColor.white.cgColor]
      // gradient.locations = [0.0, 1.0]
      gradient.startPoint = CGPoint(x: 0, y: 0)
      gradient.endPoint = CGPoint(x: 0, y: 1)
      gradientView!.layer.insertSublayer(gradient, at: 0)
      backgroundImageView.insertSubview(gradientView!, at: 0)
    }

任何有关我的错误所在的建议,我们将不胜感激。 感谢您的阅读。

David Shaw的建议下,我在configureCell()注释了这一行

  // backgroundImageView.kf.setImage(with: imageUrl)

梯度绘制正确。 我推断出我有一个计时问题,因为我是异步获取图像的。

我查看了方法签名,发现其中有一个带有完成处理程序的签名,因此我改用了它,并且可以正常工作:

  backgroundImageView.kf.setImage(with: imageUrl, completionHandler: { (image, error, cacheType, imageUrl) in
    // do stuff
  })

...所以我做到

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM