gpt4 book ai didi

swift - 在 SwiftUI 中的图像上自由手绘

转载 作者:行者123 更新时间:2023-12-05 00:46:32 25 4
gpt4 key购买 nike

我想在图像上画线。 https://stackoverflow.com/a/60002068/12759144链接有助于绘制形状。我将手势设置为图像并尝试在图像上画线。但它没有在图像上画线。

Image(uiImage: self.shareImage).renderingMode(.original).resizable().padding(5).gesture(DragGesture().onChanged( { value in self.addNewPoint(value)

                            })
.onEnded( { value in
self.isFirstTouchBegan = false
}))

DrawShape(pointss: points)
.stroke(lineWidth: 5)
.foregroundColor(.blue)

附加屏幕供引用。 enter image description here

最佳答案

这里是在 Rectangle 上绘图的快速实现,但是你可以放置任何你需要的 View 。我使用 Path 并将每个点保存在 DragGesture 中,然后使用这些点来绘制 Shape:

struct DrawExample: View {

@State var points: [CGPoint] = []

var body: some View {

ZStack {
Rectangle() // replace it with what you need
.foregroundColor(.red)
.edgesIgnoringSafeArea(.all)
.gesture(DragGesture().onChanged( { value in
self.addNewPoint(value)
})
.onEnded( { value in
// here you perform what you need at the end
}))


DrawShape(points: points)
.stroke(lineWidth: 5) // here you put width of lines
.foregroundColor(.blue)
}

}

private func addNewPoint(_ value: DragGesture.Value) {
// here you can make some calculations based on previous points
points.append(value.location)
}

}

struct DrawShape: Shape {

var points: [CGPoint]

// drawing is happening here
func path(in rect: CGRect) -> Path {
var path = Path()
guard let firstPoint = points.first else { return path }

path.move(to: firstPoint)
for pointIndex in 1..<points.count {
path.addLine(to: points[pointIndex])

}
return path
}
}

使用上面的代码,您可以实现:

enter image description here

关于swift - 在 SwiftUI 中的图像上自由手绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60000762/

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