以下代码:
let size = CGSize(width: 200, height: 30)
let rect = CGRect(origin: .zero, size: size)
let path1 = UIBezierPath()
path1.move(to: CGPoint(x: 10, y: 5))
path1.addLine(to: CGPoint(x: 180, y: 5))
path1.addArc(withCenter: CGPoint(x: 180, y: 20), radius: 15,
startAngle: (3.14159 / 2), endAngle: (3 * 3.14159 / 2), clockwise: false)
产生这个:
好的,我错过了什么吗?我不想关闭这条道路。我从不打电话给path1.close()
。我想从弧的末端添加另一条直线,而不是从它的封闭版本添加。基本上,我不希望关闭半圈,我该怎么做?
答案 0 :(得分:3)
你需要在-90度开始弧度并以+90度结束。你还需要改变它的方向。您需要执行以下操作:
path1.addArc(withCenter: CGPoint(x: 180, y: 20), radius: 15, startAngle: -.pi/2, endAngle: .pi/2, clockwise: true)
如果您想完成形状,它将如下所示:
let path1 = UIBezierPath()
path1.move(to: CGPoint(x: 10, y: 5))
path1.addLine(to: CGPoint(x: 180, y: 5))
path1.addArc(withCenter: CGPoint(x: 180, y: 20), radius: 15, startAngle: -.pi/2, endAngle: .pi/2, clockwise: true)
path1.addLine(to: CGPoint(x: 10, y: 35))
path1.addArc(withCenter: CGPoint(x: 10, y: 20), radius: 15, startAngle: .pi/2, endAngle:-.pi/2 , clockwise: true)
答案 1 :(得分:1)
希望这会帮助您实现结果
let size = CGRect(origin: .zero, size: CGSize(width : 200 , height : 30))
let path = UIBezierPath()
path.move(to: CGPoint(x: 10, y: 100))
path.addLine(to: CGPoint(x: 180, y: 100))
path.addArc(withCenter: CGPoint(x:180 , y: 85), radius: 15, startAngle: (3.14159 / 2), endAngle: (3 * 3.14159 / 2), clockwise: false)
path.addLine(to: CGPoint(x: 10, y: 70)) //y = radius * 2