// PencilView.swift
// PencilDraw
// Created by wangxuncai on 2021/12/5.
import SwiftUI
import PencilKit
struct PencilView: UIViewRepresentable {
@Binding var canvas:PKCanvasView
let toolPicker = PKToolPicker()
func makeCoordinator() -> Coordinator {
return Coordinator(parent: self)
}
func makeUIView(context: Context) -> PKCanvasView {
toolPicker.setVisible(true, forFirstResponder:canvas )
toolPicker.addObserver(canvas)
canvas.backgroundColor = .clear
canvas.delegate = context.coordinator
canvas.becomeFirstResponder()
return canvas
}
func updateUIView(_ uiView: PKCanvasView, context: Context) {
}
class Coordinator:NSObject,PKCanvasViewDelegate, PKToolPickerObserver{
var parent:PencilView
init(parent:PencilView){
self.parent = parent
}
}
}
//使用
struct CanvasBoard: View {
@State var canvas = PKCanvasView()
@Binding var image:UIImage
var body: some View {
PencilView(canvas: $canvas)
.background(Image(uiImage: image).resizable())
}
}
苹果应用商店|搜|王勋才|有我全部作品
网友评论