问题描述
我有一个绘图,我是从它自己的结构中的一组 CGPoints(即 [[CGPoints]])数组生成的(该结构本身有一些额外的数据)......我试图在我的使用以下代码查看:
class Drawing: ObservableObject {
@Published private var lines = [Line]()
@Published var currentIndex = 0
public var visibleLines: [Line] {
Array(lines.prefix(currentIndex))
}
}
class Line: ObservableObject {
var lineIndex = 0
var points = [CGPoint]()
}
Struct View: View {
@Published var drawing = Drawing()
func add(line: Line,to path: inout Path) {
let points = line.points
guard points.count > 1 else {
return
}
for i in 0 ..< points.count - 1 {
let current = points[i]
let next = points[i + 1]
path.move(to: current)
path.addLine(to: next)
}
}
var body: some View {
GeometryReader { geometry in
let frame = CGRect(x: geometry.frame(in: .local).minX,y: geometry.frame(in: .local).minY,width: geometry.frame(in: .local).width,height: geometry.frame(in: .local).height)
Path { path in
for line in drawing.visibleLines {
add(line: line,to: &path)
}
}
.scaled(for: frame)
.stroke(Color.white,linewidth: 2)
.frame(alignment: .center)
}
}
}
extension Path {
func scaled(for rect: CGRect) -> Path {
let scaleX = boundingRect.width/rect.width
print("scaleX = \(scaleX)")
let scaleY = boundingRect.height/rect.height
print("scaleY = \(scaleY)")
let scale = min(scaleX,scaleY)
print("scale = \(scale)")
return applying(CGAffineTransform(scaleX: scale,y: scale))
}
}
我试图使视图中间的绘图尽可能大,但目前它很小,并且随着用户向绘图添加点而按比例放大。我认为 CGAffineTransform 会缩放点以适合每个视图(通过我正在创建的 GeometryReader 框架),但它似乎相反。我还尝试(在我的“缩放”修饰符中)将数学切换为 rect.width/boundingRect.width 和 rect.height/boundingRect.height,但该行在视图中不可见...
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)