微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何添加阴影以使用蒙版查看

如何解决如何添加阴影以使用蒙版查看

enter image description here

我需要添加阴影以形成具有梯形形状的视图。

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

    [![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

解决方法

遮罩之外的任何物体(包括阴影)都不会被绘制。

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。