首页 > 解决方案 > 绘制 UIView 四圆

问题描述

s

如何像上面那样制作 UIView。

在下面尝试过,但它创建了一个半圆形视图。

let circlePath = UIBezierPath(arcCenter: CGPoint(x: topView.bounds.size.width, y: topView.bounds.size.height / 2), radius: topView.bounds.size.height, startAngle: .pi, endAngle: 0.0, clockwise: false)
let circleShape = CAShapeLayer()
circleShape.path = circlePath.cgPath
topView.layer.mask = circleShape

标签: iosswiftuibezierpath

解决方案


自己画吧。没有什么复杂的。

带样品: 在此处输入图像描述

  • 从中心点开始
  • 转到底部点(线)
  • 从 BottomPoint 到 LeftPoint 的弧线,角度为 Pi/2 到 Pi(顺时针方向)
  • Go to leftPoint
  • 转到中心点(线)

UIBezierPath:_

let bezierPath = UIBezierPath()
let centerPoint = CGPoint(x: view.bounds.width, y: 0)
let bottomPoint = CGPoint(x: view.bounds.width, y: view.bounds.height)
let leftPoint = CGPoint(x: 0, y: 0)
bezierPath.move(to: bottomPoint) //not needed
bezierPath.addArc(withCenter: centerPoint,
                  radius: view.bounds.height,
                  startAngle: CGFloat.pi/2.0,
                  endAngle: CGFloat.pi,
                  clockwise: true)
bezierPath.addLine(to: leftPoint) //not needed
bezierPath.addLine(to: centerPoint)

有两个“不需要”,因为它们是隐含的,但如果它们对您来说“隐藏太多”,您可能想要编写它们。

为什么是你自己?因为,一个圆只有两个点并在它之间填充。换句话说,它不会去“centerPoint”来填充它。我在手工路径中使用的角度相同的示例:

在此处输入图像描述


推荐阅读