Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

ios - addArc(withCenter) closing path

The following code:

  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)

produces this:

enter image description here

Ok, am I missing something? I do not want to close this path. I never call path1.close(). I want to add another straight line from the end of the arc, not from the closed version of it. Basically, I don't want the half circle to be closed, how can I do that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to start your arc at -90 degrees and end it at +90 degrees. You need also to change its direction. You need to do as follow:

path1.addArc(withCenter: CGPoint(x: 180, y: 20), radius: 15, startAngle: -.pi/2, endAngle: .pi/2, clockwise: true)

If you would like to complete the shape it would look like this:

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)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...