ios - 在 swift 中使用 CAShapeLayer 填充颜色不起作用
问题描述
我正在尝试下面的代码来绘制形状,这没关系,但是UIView 背景颜色没有显示,因为我给View赋予了橙色
查看我的代码:
let myView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
myView.backgroundColor = .orange
let circlePath = UIBezierPath(arcCenter: CGPoint(x: myView.bounds.size.width / 2, y: 0), radius: myView.bounds.size.height, startAngle: 0.0, endAngle: .pi, clockwise: false)
let circleShape = CAShapeLayer()
circleShape.masksToBounds = false
circleShape.path = circlePath.cgPath
circleShape.fillColor = UIColor.orange.cgColor
circleShape.strokeColor = UIColor.orange.cgColor
myView.layer.mask = circleShape
print(myView)
操场上的输出:
解决方案
试试这个代码:
let myView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
myView.backgroundColor = .orange
let circlePath = UIBezierPath(arcCenter: myView.center,
radius: myView.bounds.size.width / 2,
startAngle: 0.0,
endAngle: .pi,
clockwise: false)
let circleShape = CAShapeLayer()
circleShape.path = circlePath.cgPath
myView.layer.mask = circleShape
我认为由于错误的 arcCenter 或起点和终点天使,您看不到预期的结果
推荐阅读
- scripting - 将脚本列表作为 cron 作业运行的标准解决方案(也许是流利的?)
- sql-server - 如何解决“子查询返回超过 1 个值”错误
- c++ - 迭代器实际上指向容器中的什么?
- python - python - xml.etree.ElementTree.ParseError:格式不正确(无效令牌)
- go - Go 中来自 Kubernetes 的 Cloud SQL 连接 - 错误 403:权限不足
- python - 带有 OWFS 的 Raspberry Pi,带有 DS2482-800 板的 8 个 DS18B20 传感器,运行速度非常慢
- python - numpy.matrix 忽略复制
- reactjs - 未找到 Webpack 错误模块
- xml - 使用 XSLT 从 XML 中重命名和删除标签
- vba - VBA:是否可以将一个类创建/定义为特定的而不是另一个现有的类?