GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

How do I put Pencil into my application?

Forums > SwiftUI

@zhine  

I'm a novice. I'm new to UIKit. I want to install Pencil into my application.I need to click HB to jump to the Pencil view interface. Then display the content drawn by Pencil in the PencilCreate view. How to add (save) (redo) (exit) (undo) button to the Pencil view interface ``import SwiftUI import PencilKit

struct GrPencil: View { var body: some View { NavigationStack{

        CanvasView()
        }
      }
    }

    struct CanvasView: UIViewRepresentable {
    @State var canvasView: PKCanvasView = PKCanvasView()
    @State var toolPicker = PKToolPicker()

func makeUIView(context: Context) -> PKCanvasView {
            canvasView.drawingPolicy = .anyInput
            canvasView.becomeFirstResponder()

            toolPicker.setVisible(true, forFirstResponder: canvasView)
            toolPicker.addObserver(canvasView)

            return canvasView
        }

        func updateUIView(_ uiView: PKCanvasView, context: Context) { }
    }

Preview {

GrPencil()

} import SwiftUI import PencilKit

struct PencilCreate: View { @State var drawing = PKDrawing()

   var body: some View {
       VStack {
           VStack {
            Image(uiImage: drawing.image(from: drawing.bounds, scale: 1))
                .resizable()
               .frame(width: 200, height: 120)
               .border(Color.black)

               Button {

               }label: {
                   Text("HB")
               }
           }
       }
   }

}

Preview {

PencilCreate()

}

   

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Transform your career with the iOS Lead Essentials. Unlock over 40 hours of expert training, mentorship, and community support to secure your place among the best devs. Click for early access to this limited offer and a FREE crash course.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.