首页 > 解决方案 > 以编程方式在屏幕上快速绘制自定义椭圆形

问题描述

我怎样才能在swift(不是swiftUI)中绘制自定义椭圆形状,如下图所示。

先感谢您 在此处输入图像描述

标签: swiftuikitdraw

解决方案


我尝试使用 UIView 克隆类似的视图,并且能够创建类似的 UI,如您在屏幕截图中所述在此处输入图像描述

这是我的代码片段

let width: CGFloat = view.frame.size.width
let height: CGFloat = view.frame.size.height

let path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: self.view.bounds.size.height), cornerRadius: 0)
let rect = CGRect(x: width / 2 - 150, y: height / 2.5 - 100, width:  300, height: 400)
let circlePath = UIBezierPath(ovalIn: rect)
path.append(circlePath)
path.usesEvenOddFillRule = true

let fillLayer = CAShapeLayer()
fillLayer.path = path.cgPath
fillLayer.fillRule = .evenOdd
fillLayer.fillColor = UIColor.red.cgColor
fillLayer.opacity = 0.5
view.layer.addSublayer(fillLayer)

希望这对您有所帮助。再会。


推荐阅读