首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用蒙版在视图中添加阴影

如何使用蒙版在视图中添加阴影
EN

Stack Overflow用户
提问于 2020-10-09 17:14:00
回答 1查看 32关注 0票数 0

我需要添加阴影与形状又名梯形视图。

这是我的代码,它不能工作。

代码语言:javascript
复制
    [![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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-09 20:30:10

蒙版之外的任何东西,包括阴影,都不会被绘制。

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64277084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档