Swift UIView 绘制对角线一侧和圆角 [英] Swift UIView draw diagonal one side and round corner
问题描述
我正在用对角线绘制一些视图
I am drawing some view with diagonal side
像这样:
代码:
let layerWidth = layer.frame.width
let bezierPath = UIBezierPath()
let pointA = CGPoint(x: 0, y: 44)
let pointB = CGPoint(x: 150, y: 0)
let pointC = CGPoint(x: layerWidth, y: 0)
let pointD = CGPoint(x: layerWidth, y:44)
bezierPath.move(to: CGPoint(x: pointA.x, y: pointA.y) )
bezierPath.addLine(to: CGPoint(x: pointB.x, y: pointB.y))
bezierPath.addLine(to: pointC)
bezierPath.addLine(to: pointD)
bezierPath.close()
let shapeLayer = CAShapeLayer()
shapeLayer.path = bezierPath.cgPath
layer.addSublayer(shapeLayer)
如你所见,一切都很好,但我想让角落变圆
as you can see, everything is fine, but I want to make the corners rounded
像这样:
我怎样才能达到这个结果?
How can I achieve this result?
添加:并且收到结果后,我想把这个表单设置为另一个视图的掩码,但是有一个问题:第二个视图有边框,如果我设置自己的表单,我会丢失这个边框的一部分
ADD: and after receiving the result, I want to set this form as a mask for another view, but there is a problem: the second view has a border and if I set my own form, I lose part of this border
代码:
layer.mask = shapeLayer
错误结果:
推荐答案
Okey 我创立的方式 - https://stackoverflow.com/a/60035279/676205
Okey im founded way - https://stackoverflow.com/a/60035279/676205
只写 4 点,如:
let grayPath = UIBezierPath()
addArcs(
to: grayPath,
pointsAndRadii: [
(point: CGPoint(x: 0, y: 0), radius: 2),
(point: CGPoint(x: self.bounds.width - 104, y: 0), radius: 10),
(point: CGPoint(x: self.bounds.width, y: self.bounds.height), radius: 2),
(point: CGPoint(x: 104, y: self.bounds.height), radius: 6),
])
grayPath.close()
下一组掩码:
let shapeLayer = CAShapeLayer()
shapeLayer.path = grayPath.cgPath
layer.mask = shapeLayer
如果你有更高效的方法或者更好的性能,就写吧)
if you have more efficient way or better performance, write it )
这篇关于Swift UIView 绘制对角线一侧和圆角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!