【发布时间】:2021-08-21 03:26:04
【问题描述】:
拥有使用以下代码绘制自定义形状的代码:
struct ProgressPath: Shape {
var numberOfPoints: Int = 2
var animatableData: CGFloat {
get { CGFloat(numberOfPoints) }
set { numberOfPoints = Int(newValue) }
}
var sectionsByItems: [IndexPath] {
var result: [IndexPath] = []
for index in 0..<numberOfPoints {
guard let lastItem = result.last?.row else {
result.append(IndexPath(item: 0,
section: 0))
continue
}
let currentSection = Int(index / 2)
if index % 2 == 0 {
result.append(IndexPath(item: lastItem,
section: currentSection))
} else {
result.append(IndexPath(item: lastItem == 1 ? 0 : 1,
section: currentSection))
}
}
return result
}
func point(indexPath: IndexPath,
rect: CGRect)-> CGPoint {
let distance = rect.width - 40
return CGPoint(x: indexPath.row == 0 ? distance : 40,
y: (rect.height - 20) - 50 * (CGFloat(indexPath.section)))
}
func path(in rect: CGRect) -> Path {
var path = Path()
sectionsByItems
.forEach { indexPath in
let newPoint = point(indexPath: indexPath,
rect: rect)
if indexPath.section == 0 &&
indexPath.row == 0 {
path.move(to: newPoint)
} else {
path.addLine(to: newPoint)
}
}
return path
}
}
如下所示:
我想在创建的路径上添加形状,理想情况下,我希望有一些看起来像这样的机制,路径顶部的圆圈可以交互:
在不修改原始路径的情况下如何实现这一点有点迷茫,也许有更优雅的解决方案可以在自定义路径上绘制形状?
类似问题 - How to create game map for iOS?
注意:虽然这是上面提供的自定义路径的一个特定问题 - 我正在尝试提出一种方法,通过遵循它们的路径在任何形状的顶部绘制自定义形状并使它们可交互。
【问题讨论】:
标签: ios swift user-interface mobile swiftui