DemoCode:
import SwiftUI
import UIKit
import PencilKit
class PencilKitViewController: UIViewController, PKCanvasViewDelegate, PKToolPickerObserver {
lazy var canvasView: PKCanvasView = {
let canvasView = PKCanvasView()
canvasView.drawingPolicy = .anyInput
canvasView.translatesAutoresizingMaskIntoConstraints = false
return canvasView
}()
lazy var toolPicker: PKToolPicker = {
let toolPicker = PKToolPicker()
toolPicker.showsDrawingPolicyControls = true
toolPicker.addObserver(self)
return toolPicker
}()
let drawing = PKDrawing()
override func viewDidLoad() {
super.viewDidLoad()
canvasView.drawing = drawing
canvasView.delegate = self
view.addSubview(canvasView)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
canvasView.frame = view.bounds
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
toolPicker.setVisible(true, forFirstResponder: canvasView)
toolPicker.addObserver(canvasView)
canvasView.becomeFirstResponder()
}
// canvas
func canvasViewDrawingDidChange(_ canvasView: PKCanvasView) {
print("drawing")
}
func canvasViewDidFinishRendering(_ canvasView: PKCanvasView) {
}
func canvasViewDidEndUsingTool(_ canvasView: PKCanvasView) {
}
func canvasViewDidBeginUsingTool(_ canvasView: PKCanvasView) {
}
}
// UIRepresentable for SwiftUI
struct PencilKitView: UIViewControllerRepresentable {
class Coordinator {
var parentObserver: NSKeyValueObservation?
}
var onSubmit: ((UIImage?, Error?) -> Void)? = .none
func makeUIViewController(context: Context) -> PencilKitViewController {
let pencilKitViewController = PencilKitViewController()
context.coordinator.parentObserver = pencilKitViewController.observe(\.parent, changeHandler: { vc, _ in
})
return pencilKitViewController
}
func updateUIViewController(_ uiViewController: PencilKitViewController, context: Context) {
}
func makeCoordinator() -> Self.Coordinator { Coordinator() }
}
struct ContentView: View {
var onSubmit: ((UIImage?, Error?) -> Void)? = .none
var body: some View {
PencilKitView()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
iOS:
macCatalyst:
PencilKit
RSS for tagCapture touch input as an opaque drawing and turn it into high-quality images that can be displayed on iOS and macOS using PencilKit.
Posts under PencilKit tag
26 Posts
Sort by:
Post
Replies
Boosts
Views
Activity
In the recent WWDC 2022, FreeForm app was introduced and in that app in the tool picker there was a Fill Tool.
I want to put that fill tool (or any similar filling tool) in my xcode app that I am building. But when I use pencilkit PKToolPicker, there is not Fill Tool.
So how to get this fill tool in my app.
Any leads would be helpful. Thanks.
Hi,
can i create custom tool for PKToolPicker?
On documentation page on PKTool says "Don’t adopt this protocol in your own objects. Instead, create a tool object to provide users with the desired the tool behavior."
Best regards,
Matej Klemen
on iOS, I want to add up undo/redo and a close button. On ipadOS, I only need to add a close button
What’s your experience in adding a close button to the ToolPicker? Or at least have the position of the window so I can add an overlapping box (even on floating).
There are use cases where someone who's using an Apple Pencil may not want to enter text via Scribble. A simple example is writing "UIViewController" in a text view is unlikely to be successful.
I'd like to disable Scribble in this case and let the keyboard become the input mechanism. (Disabling Scribble system-wide in Settings is both cumbersome and overkill.)
The closest I can come to making this happen is by adding a UIScribbleInteraction on a UITextView and returning false when scribbleInteraction(shouldBeginAt:) is called.
This disables Scribble on the text view, and prevents writing from being converted into text, but the input widget still appears on screen and isn't very useful.
Here is a sample project that demonstrates the problem:
http://files.iconfactory.net/craig/bugs/Scribbler.zip
Hopefully, I'm doing something wrong here. If not, I'm happy to submit this as a FB.
-ch
Hey guys,
Is there a native bridge between a PKStrokePath and other path objects like CGPath, UIBezierPath or even SwiftUI Path ?