本文介绍了iOS PencilKit 未在 PKCanvasView 上绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In iOS Simulator, I've followed the basic steps to include PencilKit into my view controller. I got the toolKit in the UI but couldn't draw.. Not sure what I'm missing here. Sharing my code which I tried.

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

    }
}

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

Kindly let me know what I'm missing here..

推荐答案

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

This should fix your issue:

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

    }
}

这篇关于iOS PencilKit 未在 PKCanvasView 上绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-10 23:33