【问题标题】:How can i drop shadow to top and bottom of view which is inside the table view cell?如何在表格视图单元格内的视图顶部和底部放置阴影?
【发布时间】:2018-08-07 06:34:20
【问题描述】:

我在表格视图单元格中有一个视图,如果出现以下情况,我想在该视图中显示阴影

  1. 如果在第一个单元格上查看,则将阴影投射到顶部、左侧、右侧。
  2. 如果在最后一个单元格上查看,则将阴影投射到底部、左侧、右侧。
  3. 其他方式仅在左侧和右侧投影。

谢谢。

【问题讨论】:

  • 您要求投影的方式实际上并不适用,您可能需要自己绘制阴影或相应地使用重叠视图。

标签: ios swift uitableview uiview


【解决方案1】:
yourView.layer.shadowColor = UIColor.black.cgColor
yourView.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
yourView.layer.shadowOpacity = 0.5
yourView.layer.shadowRadius = 2.0

然后将下面的代码调整为您想要投射阴影的角落的方式。

var shadowRect: CGRect = yourView.bounds.insetBy(dx: 0, dy: 4) // inset top and bottom of the cell
yourView.layer.shadowPath = UIBezierPath(rect: shadowRect).cgPath

【讨论】:

    【解决方案2】:

    把这段代码放到你的班级里

    extension UIView {
    
    func dropShadow(scale: Bool = true) {
        layer.masksToBounds = false
        layer.shadowColor = UIColor.black.cgColor
        layer.shadowOpacity = 0.2
        layer.shadowOffset = CGSize.zero
        layer.shadowRadius = 5
    }}
    

    然后调用它:--

    YourView.dropShadow(scale: true)
    

    【讨论】:

      猜你喜欢
      • 2021-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-21
      • 1970-01-01
      • 2011-12-29
      相关资源
      最近更新 更多