【问题标题】:How to add shadow to view with mask如何使用蒙版添加阴影以查看
【发布时间】:2020-10-09 09:13:59
【问题描述】:

我需要为视图添加阴影,形状为梯形。

这是我的代码,但它不起作用。

    [![enter image description here][1]][1]let trapezoidPath = UIBezierPath()
    trapezoidPath.move(to: CGPoint(x: 0, y: 0))
    trapezoidPath.addLine(to: CGPoint(x: Constants.padding, y: bounds.maxY - 10))
    trapezoidPath.addLine(to: CGPoint(x: titleLabel.bounds.width + Constants.padding, y: bounds.maxY - 10))
    trapezoidPath.addLine(to: CGPoint(x: titleLabel.bounds.width + 2 * Constants.padding, y: 0))

    let trapezoidLayer = CAShapeLayer()
    trapezoidLayer.path = trapezoidPath.cgPath
    
    whiteView.layer.shadowPath = UIBezierPath(roundedRect: trapezoidPath.bounds, cornerRadius: whiteView.layer.cornerRadius).cgPath
    whiteView.layer.shadowRadius = 15
    whiteView.layer.shadowOffset = .zero
    whiteView.layer.shadowOpacity = 0.4
    whiteView.layer.masksToBounds = false
    whiteView.layer.shadowColor = UIColor.red.cgColor

    whiteView.layer.mask = trapezoidLayer

【问题讨论】:

    标签: swift uibezierpath cashapelayer


    【解决方案1】:

    你的面具之外的任何东西,包括阴影,都不会被绘制。

    要解决此问题,您需要创建两个视图。外部视图应该是透明的并且设置了阴影属性。内部视图作为子视图添加到外部视图并配置了图层蒙版。

    基本上内部视图绘制你的形状和内容,而它周围的外部视图只负责绘制阴影。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-15
      • 1970-01-01
      • 2021-04-16
      • 1970-01-01
      • 2020-01-11
      • 2013-05-02
      • 1970-01-01
      • 2018-05-19
      相关资源
      最近更新 更多