【发布时间】:2017-01-05 01:14:57
【问题描述】:
我正在尝试在 UIView 容器内(底部)添加一个贝塞尔路径(三角形),但是我似乎无法正确理解 UIView 容器的要点。我得到了这个结果(容器视图左侧似乎显示了一条三角形):
我想要一个输出,例如:
这是我迄今为止的代码:
// adding container to add image
self.topContainer.backgroundColor = UIColor(red: 49/255, green: 207/255, blue: 203/255, alpha: 1)
// self.topContainer.backgroundColor = UIColor.white
self.topContainer.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(self.topContainer)
self.topContainer.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
self.topContainer.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.55).isActive = true
self.topContainer.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
let bezierPath = UIBezierPath()
bezierPath.move(to: CGPoint(x: 0, y: self.topContainer.frame.size.height))
bezierPath.addLine(to: CGPoint(x: self.topContainer.frame.size.width, y: 222))
bezierPath.addLine(to: CGPoint(x: self.topContainer.frame.size.width, y: self.topContainer.frame.size.height))
bezierPath.addLine(to: CGPoint(x: 0, y: self.topContainer.frame.size.height))
UIColor.white.setFill()
bezierPath.fill()
bezierPath.lineWidth = 1
bezierPath.close()
let shapeLayer = CAShapeLayer()
shapeLayer.path = bezierPath.cgPath
shapeLayer.lineWidth = 2.0
shapeLayer.strokeColor = UIColor.white.cgColor
shapeLayer.fillColor = UIColor.white.cgColor
self.topContainer.layer.addSublayer(shapeLayer)
【问题讨论】:
-
??在您的第一个屏幕截图中,我没有看到任何蓝色或红色。
-
它应该是我实际 UIView 容器上绿色顶部的白色三角形,因为 setFill 应该是 UIColor.white.setFill()。即使我认为我的线路连接正确,它也没有显示? @马特
-
“它应该是绿色顶部的白色三角形”然后你在蓝色顶部显示一个红色三角形是很愚蠢的!
-
它已更改,先生@matt
-
是的,现在。我建议你在开始提问时应该非常小心。花时间理解并提供所需的信息。
标签: ios swift uiview uibezierpath