参考链接
效果:
代码:
let bezierPath = UIBezierPath()
bezierPath.move(to: CGPoint(x: 40, y: 40))
bezierPath.addLine(to: CGPoint(x: 150, y: 40))//添加线条
bezierPath.addArc(withCenter: CGPoint(x: 170, y: 40), radius: 20, startAngle: CGFloat(-1.0*M_PI), endAngle: CGFloat(-1.5*M_PI), clockwise: false)//添加圆弧
bezierPath.addLine(to: CGPoint(x: 170, y: 150))
bezierPath.addArc(withCenter: CGPoint(x: 170, y: 170), radius: 20, startAngle: CGFloat(-0.5*M_PI), endAngle: CGFloat(-M_PI), clockwise: false)
bezierPath.addLine(to: CGPoint(x: 40, y: 170))
bezierPath.close()
let shapeLayer = CAShapeLayer()
let gradientLayer = CAGradientLayer()
shapeLayer.path = bezierPath.cgPath
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.strokeColor = UIColor.black.cgColor
self.layer.addSublayer(shapeLayer)