如何获得当前选择的铅笔套件颜色?

How do I get the currently selected pencil kit color?

我有以下铅笔套件设置:

class PencilKitVC: UIViewController {

    // MARK: - Views

    private let canvasView = PKCanvasView()
    private let toolPicker = PKToolPicker()

    // MARK: - View Lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()

        setupViews()
    }

    // MARK: - Setup

    private func setupViews() {
        view.backgroundColor = .lightGray

        canvasView.backgroundColor = .white
        // Support finger and pencil
        canvasView.drawingPolicy = .anyInput

        view.addSubview(canvasView)
        canvasView.snp.makeConstraints {
            [=10=].leading.trailing.top.equalToSuperview().inset(16.0)
            [=10=].bottom.equalTo(view.safeAreaLayoutGuide).inset(16.0)
        }

        canvasView.minimumZoomScale = 0.5
        canvasView.maximumZoomScale = 4.0

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

}

是否可以通过代码获取PKToolPicker中当前选中的颜色?我需要这种颜色,以便我可以在我添加的文本字段功能上使用它。

您需要为工具选择器添加观察者。那就听听吧

toolPicker.addObserver(self)

func toolPickerSelectedToolDidChange(_ toolPicker: PKToolPicker) {
    if let inkingTool = toolPicker.selectedTool as? PKInkingTool {
        let color = inkingTool.color
    }
}

您可以从官方文档中获取更多信息:- here.