【问题标题】:Programmatically drawing custom oval shape on screen swift以编程方式在屏幕上快速绘制自定义椭圆形
【发布时间】:2020-08-07 07:32:32
【问题描述】:

如何在 swift(不是 swiftUI)中绘制自定义椭圆形,如下图所示。

提前谢谢你

【问题讨论】:

  • 寻找 UIBezierPath,在你的情况下一个带角半径的矩形应该足够了。
  • 你能分享一些代码sn-ps吗?这对我有很大帮助
  • 你可以只使用cornerRadius ...

标签: swift uikit draw


【解决方案1】:

我曾尝试使用 UIView 克隆类似的视图,并且能够创建类似的 UI,正如您在屏幕截图 中所说的那样

这是我的代码 sn-p

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)

希望这对您有所帮助。美好的一天。

【讨论】:

    【解决方案2】:

    你可以像这样画椭圆路径

    class CustomOval: UView {
    
        override func drawRect(rect: CGRect) {
           var ovalPath = UIBezierPath(ovalIn: rect)
            UIColor.gray.setFill()
            ovalPath.fill()
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      • 2015-11-17
      • 2019-08-14
      • 1970-01-01
      • 1970-01-01
      • 2013-04-01
      • 1970-01-01
      相关资源
      最近更新 更多