iOS PencilKit 未在 PKCanvasView 上绘图

iOS PencilKit not drawing on the PKCanvasView

在 iOS 模拟器中,我按照基本步骤将 PencilKit 包含到我的视图控制器中。我在 UI 中获得了工具包,但无法绘制。不确定我在这里遗漏了什么。分享我尝试过的代码。

import UIKit
import PencilKit

class DrawingBoardViewController: UIViewController {

@IBOutlet weak var canvasView: PKCanvasView!

 override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)       
        setupCanvasView()
    }

 func setupCanvasView() {
                
        let toolPicker = PKToolPicker.init()
        toolPicker.setVisible(true, forFirstResponder: canvasView)
        toolPicker.addObserver(canvasView)
        toolPicker.isRulerActive = false
        canvasView.isOpaque = true
        canvasView.becomeFirstResponder()
        canvasView.drawingPolicy = .anyInput
        
    }
}

请让我知道我在这里遗漏了什么..

这应该可以解决您的问题:

import UIKit
import PencilKit

class DrawingBoardViewController: UIViewController {

@IBOutlet weak var canvasView: PKCanvasView!

let toolPicker = PKToolPicker.init()

 override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)       
        setupCanvasView()
    }

 func setupCanvasView() {
                
        toolPicker.setVisible(true, forFirstResponder: canvasView)
        toolPicker.addObserver(canvasView)
        toolPicker.isRulerActive = false
        canvasView.isOpaque = true
        canvasView.becomeFirstResponder()
        canvasView.drawingPolicy = .anyInput
        
    }
}