需求如下:
- 加载用户用 markdown 写的内容,用
swift-markdown-ui - 然后用
PencilKit来绘制
想要实现在 swift 得到的试图上来用手写笔绘制。 感觉应该是有点像 photoshop 的图层,书写的内容应该是透明的在题目的上方。 关于 goodnote 和 notebility 的实现,有什么好的介绍么?
但是这个半透明的层应该如何实现呢?
//
// ContentView.swift
// DrawingDemos
//
// Created by meng xianlang on 2023/8/17.
//
**import** SwiftUI
**import** PencilKit
**import** MarkdownUI
**struct** ContentView: View {
**var** body: **some** View {
Home()
}
}
**struct** Home : View {
@State **var** canvas = PKCanvasView()
@State **var** isDraw = **true**
@State **var** isPresented : Bool = **false**
@State **var** color : Color = .black
**var** body: **some** View {
NavigationStack {
ZStack {
Markdown("""
```
class HelloWorld
{
public static void main
{
}
}
```
""")
.background(Color.white.opacity(0))
DrawingView(canvas: $canvas, isDraw: $isDraw, color: $color)
.navigationTitle("hells")
.background(Color.white.opacity(0))
Button {
isDraw.toggle()
} label: {
Image(systemName: "pencil.slash")
}
.font(.title)
ColorPicker("adf", selection: $color)
}
}
}
// NavigationView {
// DrawingView(canvas: $canvas, isDraw: $isDraw)
// }
// .navigationTitle("Drawing")
// .navigationBarTitleDisplayMode(.inline)
// .navigationBarItems(leading: Button(
// action: {
//
// },
// label: {
// Image(systemName: "square.and.arrow.down.fill")
// .font(.title)
// }
// ), trailing: HStack(spacing: 15){
// Button {
// isDraw.toggle()
// } label: {
//
//
// Image(systemName: "pencil.slash")
// }
// }
// )
//
// }
}
**struct** DrawingView : UIViewRepresentable {
@Binding **var** canvas : PKCanvasView
@Binding **var** isDraw : Bool
@Binding **var** color : Color
**var** ink : PKInkingTool {
PKInkingTool(.pen, color: UIColor(color))
}
**let** eraser = PKEraserTool(.bitmap )
**func** makeUIView(context: Context) -> PKCanvasView {
canvas.drawingPolicy = .anyInput
canvas.tool = isDraw ? ink : eraser
**return** canvas
}
**func** updateUIView(_ uiView: PKCanvasView, context: Context) {
uiView.tool = isDraw ? ink : eraser
}
}
**struct** ContentView_Previews: PreviewProvider {
**static** **var** previews: **some** View {
ContentView()
}
}
但是目前,不能同时正常的显示 markdwon 和用户涂写的内容。
这就非常奇怪了