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
4.8k views
in Technique[技术] by (71.8m points)

ios - CALayer: create broken ellipse with round edges?

Here is my code to add an elliptic CALayer to a UIView:

let circleLayer = CAShapeLayer()
circleLayer.path = UIBezierPath(arcCenter: CGPoint(x: frame.size.width / 2, y: frame.size.height / 2),
                                radius: frame.size.width / 2,
                                startAngle: -.pi / 2,
                                endAngle: .pi / 6,
                                clockwise: false).cgPath
circleLayer.lineWidth = 8.0
circleLayer.cornerRadius = 4.0
circleLayer.strokeColor = UIColor.black.cgColor
circleLayer.fillColor = UIColor.clear.cgColor
circleLayer.lineJoin = .round
squareView.layer.backgroundColor = UIColor.clear.cgColor
squareView.layer.addSublayer(circleLayer)

The result I get :

enter image description here

However, I would like to get this result instead:

enter image description here

Could you help me getting this?

Thank you for your help!


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

1 Reply

0 votes
by (71.8m points)

To make your line caps round, simply tell your shape layer to use round line caps:

circleLayer.lineCap = .round

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

...