Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable pencil drawing in PKCanvasView of PencilKit, just for viewing purpose

I successfully created PKCanvasView in my view and make it work to draw in there. But how to disable drawing in PKCanvasView for some purposes like viewing mode only.

Is it possible to disable drawing with both pencil and finger in PKCanvasView?

I can disable finger drawing with just setting the variable allowsFingerDrawing false, but I want to disable pencil too.

like image 829
Munkhsoyol Ganbat Avatar asked Oct 24 '25 02:10

Munkhsoyol Ganbat


2 Answers

The solution - canvasView.isUserInteractionEnabled = false - will also disable the scrolling features of the canvaseView.

A better solution could simply disable the drawing gesture

self.canvasView.drawingGestureRecognizer.isEnabled = false

This will turn off both pen and finger drawing but keep the scroll view features available.

like image 182
Log.O Avatar answered Oct 26 '25 09:10

Log.O


Applies to iOS 13 and iOS 14

To answer your question, iOS 14's drawingPolicy did not provide a 'noInput' option. But an easy alternative would be to set isUserInteractionEnabled.

For example, a PKCanvasView that is declared as canvasView could be set with isUserInteractionEnabled to restrict all input:

canvasView.isUserInteractionEnabled = false

When the user is in pencil-only mode, this will also restrict the finger's long-press from inserting white space, cutting, copying, deleting, and duplicating.

Setting isUserInteractionEnabled will also restrict the two-finger scrolling on the canvas.

like image 40
Marcy Avatar answered Oct 26 '25 09:10

Marcy