Swift 今更!?PencilKit

WWDC2020「PencilKitの新機能」を見て、PencilKitを使っていないと思い立ち始める。
PencilKitを使って作りたいものが過去からあり、そこに向かって、最初の最初、スタートライン前。

とりあえずPencilKitを使う

StoryBoardでPKCanvasView配置

ViewController.swift

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import UIKit
import PencilKit
 
class ViewController: UIViewController, PKToolPickerObserver {
 
    @IBOutlet weak var pkCanvasView: PKCanvasView!
 
    var toolPicker: PKToolPicker!
 
    override func viewDidLoad() {
        super.viewDidLoad()
        pkCanvasView.allowsFingerDrawing = false
    }
     
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
     
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        if let window = self.pkCanvasView.window {
            let toolPicker = PKToolPicker.shared(for: window)
            toolPicker?.addObserver(pkCanvasView)
            toolPicker?.setVisible(true, forFirstResponder: pkCanvasView)
            pkCanvasView.becomeFirstResponder()
            print("PKToolPicker Set")
        }
    }
}

これだけでここまで出来る

Swift

Posted by shi-n