This is my SwiftUI code, it works fine. user can draw and pres button with no problem. i want user draws on anything, labels, buttons etc. But how can i do this with UIKit ? any idea ?
(i edited Kavsoft's code pencilkit for swiftui: https://kavsoft.tech/SwiftUI_2.0/Pencil_Kit/)
import SwiftUI
import PencilKit
struct ContentView: View {
var body: some View {
myContentView()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
myContentView()
}
}
struct myContentView: View {
@State var canvas = PKCanvasView()
@State var isDraw = true
@State var color : UIColor = .black
@State var type : PKInkingTool.InkType = .pen
@State var colorPicker = false
@State var mytextSize = CGFloat(UIScreen.main.bounds.height * 0.032)
@State var kalemlikSize = CGFloat(UIScreen.main.bounds.height * 0.038)
@State private var selectedFrameworkIndex = 0
var body: some View {
VStack{
HStack{ //MARK: HEADER
Spacer()
Button(action: {
}) {
Text("Title").foregroundColor(.black).font(.title)
}
Spacer()
}.padding(5)
ZStack{
PKCanvasRepresentation(canvas: $canvas, isDraw: $isDraw,type: $type,color: $color)
VStack(alignment: .leading, spacing: 10){
//Spacer()
Text("Label1")
Text("Label2").font(.title)
Button(action:{ // MARK: Şık A
print("button pressed")
}){
Text("my blue button")
Spacer()
}.padding(5)
Spacer()
HStack(alignment:.bottom, spacing: 20){
Spacer().frame(width: 0.1)
Group{
Spacer()
Button(action: {
self.canvas.isUserInteractionEnabled = true
self.type = .pen
self.isDraw = true
self.color = .black
}) {
Image(systemName: "pencil.tip")
.resizable().aspectRatio(contentMode: .fit).frame(height: kalemlikSize, alignment: .center).accentColor(.black)
}
Button(action: {
self.canvas.drawing = PKDrawing()
}) {
Image(systemName: "trash").resizable().aspectRatio(contentMode: .fit).frame(height: mytextSize * 1.2, alignment: .bottom)
}
Spacer()
}
Spacer().frame(width: 0.1)
}
}.padding(5)
}
}
}
}
struct PKCanvasRepresentation : UIViewRepresentable {
@Binding var canvas : PKCanvasView
@Binding var isDraw : Bool
@Binding var type : PKInkingTool.InkType
@Binding var color : UIColor
var ink : PKInkingTool{
PKInkingTool(type, color: color )
}
let eraser = PKEraserTool(.vector)
func makeUIView(context: Context) -> PKCanvasView{
canvas.tool = isDraw ? ink : eraser
return canvas
}
func updateUIView(_ uiView: PKCanvasView, context: Context) {
uiView.tool = isDraw ? ink : eraser
}
}
This is my SwiftUI code, it works fine. user can draw and pres button with no problem. i want user draws on anything, labels, buttons etc. But how can i do this with UIKit ? any idea ?
If your code works fine you can just put your code into a UIHostingController
import SwiftUI
class SwiftUI_ViewController: UIViewController {
let contentView = UIHostingController(rootView: myContentView())
override func viewDidLoad() {
addChild(contentView)
view.addSubview(contentView.view)
}
}
Then initialize the UIViewController
as you would any other UIViewController
.
Note: Adjusting the constraints in the Storyboard
or the UIViewController
might be necessary.