ios - 如何绘制与弧垂直相交的线(核心图形)?

标签 ios swift core-graphics

我想在圆上垂直画一条线(截取圆弧),如图所示。

我正在使用这段代码画圆

let center = CGPoint(x: bounds.width / 2, y: bounds.height / 2)

let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: Conversion.degreesToRadians(value: CGFloat(0)), endAngle: Conversion.degreesToRadians(value: CGFloat(360)), clockwise: true)
path.lineWidth = 2
path.lineCapStyle = CGLineCap.square
UIColor.white.setStroke()
path.stroke()

enter image description here

最佳答案

基本思想是圆以CGPoint为中心有一定的半径。要找出圆上的一个点,您可以像这样计算 x 和 y 坐标:

func point(center: CGPoint, radius: CGFloat, angle: CGFloat) -> CGPoint {
    let x = center.x + radius * cos(angle)
    let y = center.y + radius * sin(angle)
    return CGPoint(x: x, y: y)
}

角度以弧度为单位,从 3 点钟方向开始,顺时针方向。

所以那些垂直相交的笔划只是两个 CGPoint 之间给定角度的线段,例如,线段起点的“半径”可能小于圆的半径。对于直线的终点,使用相同的角度,但使用的半径值刚好大于圆的半径。

关于ios - 如何绘制与弧垂直相交的线(核心图形)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45158467/

相关文章:

cocoa-touch - 带有核心图形绘图的核心动画

Ios绘图线动画

ios - 如何像 ibooks 样式一样自定义 UISlide?

ios - xcode 错误无法找到具有您所属的给定团队 ID 的团队。

ios - 在哪些情况下可以 viewWillAppear(_ animated : Bool) method will not be called even that viewDidLoad() is already called?

ios - 将字符替换为 url 可接受的格式 [iOS/SWIFT]

ios - Eureka 表格 : How to change form global tint colour

arrays - 计算数组中的数字并按 swift 计数对它们进行排序

swift - 为什么要编译?在另一个函数中声明的函数

iphone - iOS -- 转换PDF导致颜色饱和