gpt4 book ai didi

swift - 虚线 UIBezierPath 子路径连接点

转载 作者:行者123 更新时间:2023-11-28 12:32:37 27 4
gpt4 key购买 nike

我使用一些通过的点创建虚线 UIBezierPath。

let pathLayer = CAShapeLayer()
pathLayer.strokeColor = UIColor.blue.cgColor
pathLayer.lineWidth = 3.0
pathLayer.fillColor = UIColor.clear.cgColor
pathLayer.lineDashPattern = [5.0, 2.0]
pathLayer.lineDashPhase = 0

let path = PathCreator.createPath(resultPoints)

pathLayer.path = path.cgPath

我已经简化了 PathCreator,但它看起来完全像这样

PathCreator {
func createPath(resultPoints: [CGPoint]) -> UIBezierPath {
let path = UIBezierPath()
for i in 0..<(resultPoints.count-1) {
if resultPoints[i] is CircleCenterPoint {
continue
}
if resultPoints[i] is ArcEndPoint {
path.move(to: resultPoints[i].cg())
path.addLine(to: resultPoints[i+1].cg())
}
if let arcStartPoint = resultPoints[i] as? ArcStartPoint,
let circleCenterPoint = resultPoints[i+1] as? CircleCenterPoint,
let arcEndPoint = resultPoints[i+2] as? ArcEndPoint {
path.addArc(withCenter: circleCenterPoint.cg(),
radius: circleCenterPoint.radius,
startAngle: arcStartPoint.arcStartAngle,
endAngle: arcEndPoint.arcEndAngle,
clockwise: circleCenterPoint.clockwise)
} else {
path.move(to: resultPoints[i].cg())
path.addLine(to: resultPoints[i+1].cg())
}
}
return path
}
}

但有时我在子路径的交界处遇到问题:

path example

我错过了什么?

最佳答案

仅对第一个点使用“path.move”。对于其余点,请使用“path.addLine”。

所以解决方案如下:

PathCreator {
func createPath(resultPoints: [CGPoint]) -> UIBezierPath {
var wMoved = false;
let path = UIBezierPath()
for i in 0..<(resultPoints.count-1) {
if resultPoints[i] is CircleCenterPoint {
continue
}
if resultPoints[i] is ArcEndPoint {
if wMoved {
path.addLine(to: resultPoints[i].cg())
} else {
path.move(to: resultPoints[i].cg())
wMoved = true
}
path.addLine(to: resultPoints[i+1].cg())
}
if let arcStartPoint = resultPoints[i] as? ArcStartPoint,
let circleCenterPoint = resultPoints[i+1] as? CircleCenterPoint,
let arcEndPoint = resultPoints[i+2] as? ArcEndPoint {
path.addArc(withCenter: circleCenterPoint.cg(),
radius: circleCenterPoint.radius,
startAngle: arcStartPoint.arcStartAngle,
endAngle: arcEndPoint.arcEndAngle,
clockwise: circleCenterPoint.clockwise)
} else {
if wMoved {
path.addLine(to: resultPoints[i].cg())
} else {
path.move(to: resultPoints[i].cg())
wMoved = true
}
path.addLine(to: resultPoints[i+1].cg())
}
}
return path
}
}

关于swift - 虚线 UIBezierPath 子路径连接点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41703230/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com