iOS PencilKit not drawing
Asked Answered
C

1

2

I am trying to PencilKit and but I cannot draw anything in the app. I set my code as below.

import UIKit
import PencilKit

class DrawingViewController: UIViewController {

  var canvasView: PKCanvasView!

  override func viewDidLoad() {
    super.viewDidLoad()

    let canvasView = PKCanvasView(frame: view.bounds)
    canvasView.allowsFingerDrawing = false
    view.addSubview(canvasView)

    canvasView.translatesAutoresizingMaskIntoConstraints = false
    canvasView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    canvasView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    canvasView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    canvasView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true

    canvasView.backgroundColor = UIColor.lightGray
    canvasView.tool = PKInkingTool(.pen, color: .black, width: 10)
  }

}

I am expecting to draw / inking in the canvasView but there's no response when I try the app. My device is running in 13.2 so there's no problem with the target. I've also downloaded the Apple's app from WWDC 2019 session but it's not working in the simulator. For clarify I've also tested it in real device but not respond in any application with PencilKit.

Coonhound answered 17/1, 2020 at 5:53 Comment(0)
O
6

The canvasView.allowsFingerDrawing = false may have been the problem if using a finger or mouse as input rather than an Apple pencil. Apple pencil does not currently work with iPhones if trying that combination. The code looks fine otherwise.


These are the iOS settings for drawing with PencilKit

iOS 14

Use the drawingPolicy setting to enable drawing by input type

Apple pencil only:

canvasView.drawingPolicy = .pencilOnly

Finger, mouse cursor(iOS simulator) and Apple pencil:

canvasView.drawingPolicy = .anyInput

Allows the user to set the desired input with Pencil Kit's toolpicker toggle:

canvasView.drawingPolicy = .default

In the Settings app there is a global setting called "Only Draw with Apple Pencil". This can be read from UIPencilInteraction.prefersPencilOnlyDrawing in PencilKit.


iOS 13 (deprecated in iOS14)

To draw with the Apple pencil only:

canvasView.allowsFingerDrawing = false

To draw with all input types:

canvasView.allowsFingerDrawing = true
Odalisque answered 25/6, 2020 at 2:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.