问题描述
我正在用对角线绘制一些视图
像这样:
代码:
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)
如你所见,一切都很好,但我想让角落变圆
像这样:
我怎样才能达到这个结果?
添加: 并且收到结果后,我想把这个表单设置为另一个视图的掩码,但是有一个问题:第二个视图有边框,如果我设置自己的表单,我会丢失这个边框的一部分
代码:
layer.mask = shapeLayer
错误结果:
解决方法
Okey 是我创立的方式 - 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,radius: 10),(point: CGPoint(x: self.bounds.width,y: self.bounds.height),(point: CGPoint(x: 104,radius: 6),])
grayPath.close()
下一组掩码:
let shapeLayer = CAShapeLayer()
shapeLayer.path = grayPath.cgPath
layer.mask = shapeLayer
如果你有更高效的方法或者更好的性能,就写吧)