简体   繁体   中英

How do I pan PKCanvasView?

I have a PKCanvasView setup as follows:

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 {
            $0.leading.trailing.top.equalToSuperview().inset(16.0)
            $0.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()

        let canvasButton = UIButton()
        canvasButton.setTitle("Canvas", for: .normal)
        canvasButton.setTitleColor(.blue, for: .normal)
        view.addSubview(canvasButton)
        canvasButton.snp.makeConstraints {
            $0.leading.top.equalToSuperview().inset(32.0)
        }
    }

}

Zooming works however I cannot pan. How can I enable panning?

Try adding following in your code : -

    canvasView.drawing = PKDrawing()
    canvasView.tool = PKInkingTool(.pen, color: .black, width: 1)

Hope this helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM