问题
I am drawing some view with diagonal side
like this:
code:
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
like this:
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
Code:
layer.mask = shapeLayer
wrong result:
回答1:
Okey im founded way - https://stackoverflow.com/a/60035279/676205
just write 4 point like:
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()
next set mask:
let shapeLayer = CAShapeLayer()
shapeLayer.path = grayPath.cgPath
layer.mask = shapeLayer
if you have more efficient way or better performance, write it )
来源:https://stackoverflow.com/questions/65970406/swift-uiview-draw-diagonal-one-side-and-round-corner