【问题标题】:UITableViewCell shadow above the view on simulator but below in hierarchyUITableViewCell 阴影在模拟器视图上方但在层次结构下方
【发布时间】:2022-01-03 15:07:14
【问题描述】:

在我的自定义 UITableViewCell 中,我添加了 ShadowView 并将我的所有内容放在上面。当我运行它时,我在单元格上方看到了这个阴影,但它的层次结构正确地位于单元格下方。当我滚动到这个单元格时,它正在发生变化。有什么问题?

class ShadowView: UIView {
        var layer0 = CAShapeLayer()
        var layer1 = CAShapeLayer()
        var layer2 = CAShapeLayer()
        
        init() {
            super.init(frame: .zero)
        }
        
        required init?(coder: NSCoder) {
            super.init(coder: coder)
        }
    
        override func layoutSubviews() {
            super.layoutSubviews()
            configShadow()
        }
        
        private func configShadow() {
            let shadowPath0 = UIBezierPath(roundedRect: bounds, cornerRadius: 16)
            layer0.shadowPath = shadowPath0.cgPath
            layer0.shadowColor = UIColor.red.withAlphaComponent(0.03).cgColor
            layer0.shadowOpacity = 1
            layer0.shadowRadius = 5.32
            layer0.shadowOffset = CGSize(width: 0, height: 4.72)
            layer0.bounds = bounds
            layer0.position = center
            superview?.layer.insertSublayer(layer0, below: layer)
    
            let shadowPath1 = UIBezierPath(roundedRect: bounds, cornerRadius: 16)
            layer1.shadowPath = shadowPath1.cgPath
            layer1.shadowColor = UIColor.red.withAlphaComponent(0.04).cgColor
            layer1.shadowOpacity = 1
            layer1.shadowRadius = 17.87
            layer1.shadowOffset = CGSize(width: 0, height: 15.86)
            layer1.bounds = bounds
            layer1.position = center
            superview?.layer.insertSublayer(layer1, below: layer)
    
            let shadowPath2 = UIBezierPath(roundedRect: bounds, cornerRadius: 16)
            layer2.shadowPath = shadowPath2.cgPath
            layer2.shadowColor = UIColor.red.withAlphaComponent(0.07).cgColor
            layer2.shadowOpacity = 1
            layer2.shadowRadius = 80
            layer2.shadowOffset = CGSize(width: 0, height: 71)
            layer2.bounds = bounds
            layer2.position = center
            superview?.layer.insertSublayer(layer2, below: layer)
        }
    }

【问题讨论】:

  • “但它的层次结构正确地位于单元格下方”是什么意思? “当我滚动到此单元格时,它正在发生变化”是什么意思?
  • 当我捕获视图层次结构时,我看到了视图下方的阴影。应该是这样的。但是在设备上我看到了视图上方的阴影,但是当我在 tableView 之外制作这个单元格的一部分时,阴影变得正确(我假设在 prepareForReuse 之后)
  • 我发现了一个小问题。 func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { if cell.isKind(of: WalletBalanceTableViewCell.self) { tableView.bringSubviewToFront(cell) } } 现在我在视图下方看到阴影,但在 prepareForReuse 之后(?) 再次
  • 您如何使用“上方”和“下方”这两个术语?它们在这里是什么意思?
  • Above 表示阴影与我的视图完全重叠(就像顶部的另一个视图)。下面 - 视图正确地投射阴影,超出视图的边界,并且视图保持我需要的颜色。

标签: shadow cashapelayer


【解决方案1】:

我认为您对insertSublayer(layer0, below: layer) 的使用涉及错误的期望。你不能把这个子层放在视图自己的层后面!使用形状层仅在视图之外投射阴影的正确方法是使用第二个视图,在第一个视图后面,它包含形状层。这样,第一个视图可以很好地真正覆盖第二个视图区域(因此它永远不会被看到),并且只有阴影可以从第一个视图后面“泄漏”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 2021-02-19
    • 2012-03-03
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    相关资源
    最近更新 更多