Ios 根据单元格索引路径向表视图单元格元素添加渐变层
我很难在表格视图单元格中添加渐变层作为背景元素。渐变层取决于单元索引路径。我有一个Ios 根据单元格索引路径向表视图单元格元素添加渐变层,ios,swift,uitableview,gradient,Ios,Swift,Uitableview,Gradient,我很难在表格视图单元格中添加渐变层作为背景元素。渐变层取决于单元索引路径。我有一个enum颜色,它使用indepath作为它的rawValue。所需的输出很简单,如 这是创建渐变层的函数。我用它来写函数 func setGradientBackground(colorOne: UIColor, colorTwo: UIColor, name: String) { let gradient = CAGradientLayer() gradient.frame = bounds
enum
颜色,它使用indepath
作为它的rawValue
。所需的输出很简单,如
这是创建渐变层的函数。我用它来写函数
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)
}
为了向表格视图单元格中的元素添加渐变,我们尝试了几种方法。目前我正在通过可见单元格循环添加图层。我已尝试从视图willlayoutsubviews
调用。这是我唯一一次看到应用渐变
我看到了两个问题。该应用程序变得不可用,因为我认为当视图中的某些内容发生变化时,该函数或多或少会被连续调用。同时,这些层被绘制在单元格中的所有其他部分上
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)
}
}
}
我曾尝试在cellForRowAt
中添加图层,并且将显示单元格
,但没有成功。我相信这是因为细胞还没有“存在”,所以添加一层没有任何区别
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问题,我应该检查单元格是否已经有层,以避免不断添加它。(我无法链接该问题,因为我现在找不到它)
关于如何获得此工作欢迎的任何建议,谢谢。请尝试使用
在ViewDidLoad()中添加以下代码
试一试
在ViewDidLoad()中添加以下代码
对于任何可能觉得这很有用的人来说,让它工作所需的更改
使用本建议的视觉效果,我可以让它工作。我从来没有使用过视觉效果,所以不知道它到底为什么会起作用。我可能还需要一些改进
在自定义表格单元格类中,我添加了一个UIVisualEffectView
。在其内容视图中,我添加了我想要更改其背景的元素
将任何子视图添加到视觉效果视图的contentView属性中。
不要将子视图直接添加到视觉效果视图本身
回到控制器中,我使用这个函数将渐变添加到视觉效果视图中,该视图包含索引和单元格
func addGradient(_ i: Int, _ cell: GroupTableCell) {
guard let colorOne = Colors(rawValue: i)?.classicColor,
let colorTwo = Colors(rawValue: i)?.alternativeColor else {return}
cell.visualEffectView.contentView.setGradientBackground(colorOne: colorOne,
colorTwo: colorTwo,
name: cellLayerName)
}
我仍然必须向视图willlayoutsubviews
添加一个函数。这是为了配置cellForRowAt
没有的单元格。属性setGradients
最初设置为true,但随后立即设置为false,因此不像以前那样连续调用
var setGradients = true
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
if setGradients {
configTableCells()
setGradients = false
}
}
func configTableCells() {
guard let cells = self.tableView.visibleCells as? [GroupTableCell] else {return}
for (i, cell) in cells.enumerated() {
if shouldAddSubLayer(cell) {
addGradient(i, cell)
}
}
}
最后,在cellForRowAt
中,这似乎解决了其余的问题,一切似乎都顺利运行,没有延迟
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: GROUP_CELL_ID, for: indexPath) as! GroupTableCell
cell.groupNameLabel.text = "Group Name"
cell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
addGradient(indexPath.row, cell)
return cell
}
我尝试使用UIView
,而不是UIVisualEffectView
。但是渐变会再次覆盖单元格内容。因此,需要一个UIVisualEffectView
,对于任何可能发现此功能有用的人,都需要进行必要的更改以使其正常工作
使用本建议的视觉效果,我可以让它工作。我从来没有使用过视觉效果,所以不知道它到底为什么会起作用。我可能还需要一些改进
在自定义表格单元格类中,我添加了一个UIVisualEffectView
。在其内容视图中,我添加了我想要更改其背景的元素
将任何子视图添加到视觉效果视图的contentView属性中。
不要将子视图直接添加到视觉效果视图本身
回到控制器中,我使用这个函数将渐变添加到视觉效果视图中,该视图包含索引和单元格
func addGradient(_ i: Int, _ cell: GroupTableCell) {
guard let colorOne = Colors(rawValue: i)?.classicColor,
let colorTwo = Colors(rawValue: i)?.alternativeColor else {return}
cell.visualEffectView.contentView.setGradientBackground(colorOne: colorOne,
colorTwo: colorTwo,
name: cellLayerName)
}
我仍然必须向视图willlayoutsubviews
添加一个函数。这是为了配置cellForRowAt
没有的单元格。属性setGradients
最初设置为true,但随后立即设置为false,因此不像以前那样连续调用
var setGradients = true
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
if setGradients {
configTableCells()
setGradients = false
}
}
func configTableCells() {
guard let cells = self.tableView.visibleCells as? [GroupTableCell] else {return}
for (i, cell) in cells.enumerated() {
if shouldAddSubLayer(cell) {
addGradient(i, cell)
}
}
}
最后,在cellForRowAt
中,这似乎解决了其余的问题,一切似乎都顺利运行,没有延迟
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: GROUP_CELL_ID, for: indexPath) as! GroupTableCell
cell.groupNameLabel.text = "Group Name"
cell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
addGradient(indexPath.row, cell)
return cell
}
我尝试使用UIView
,而不是UIVisualEffectView
。但是渐变会再次覆盖单元格内容。因此需要一个UIVisualEffectView谢谢ben,需要一些修改,但似乎可以正常工作,谢谢!谢谢ben,需要进行一些修改,但似乎可以正常工作,谢谢!