使用swift写一个画椭圆形的小程序
以下是使用Swift编写画椭圆形的代码:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let ovalPath = UIBezierPath(ovalIn: CGRect(x: 50, y: 50, width: 200, height: 100))
let ovalLayer = CAShapeLayer()
ovalLayer.path = ovalPath.cgPath
ovalLayer.strokeColor = UIColor.red.cgColor
ovalLayer.lineWidth = 2.0
ovalLayer.fillColor = UIColor.clear.cgColor
self.view.layer.addSublayer(ovalLayer)
}
}
在这个例子中,我们使用UIBezierPath创建一个椭圆形路径,并将其添加到CAShapeLayer中。然后,我们设置图层的颜色、宽度和填充颜色,并将其添加到视图的层级结构中。最终,我们会得到一个红色的椭圆形,宽度为2个点,空心的。您可以根据需要更改椭圆形的位置、大小、颜色和线宽
原文地址: https://www.cveoy.top/t/topic/djwm 著作权归作者所有。请勿转载和采集!