「Swift 今更!?PencilKit」のSwiftUI・iPadOS14.5版

Swift 今更!?PencilKit

以前の記事「Swift 今更!?PencilKit」

SwiftUI・iPadOS14.5版

リフィル画像を背景に設定版

  • リフィル画像はAssetに設定。
  • Device OrientationをiPad・Landscapeのみ設定。

import SwiftUI
import PencilKit

struct ContentView: View {
    @State var pkcanvasview: PKCanvasView = PKCanvasView()

    let screenSize: CGSize = UIScreen.main.bounds.size

    var body: some View {
        ZStack {
            Image("template")
                .resizable()
                .scaledToFit()
                .frame(width: screenSize.width, height: screenSize.height, alignment: .center)
            PencilKitCanvasView(pkcanvasview: self.$pkcanvasview)
        }
    }
}

struct PencilKitCanvasView: UIViewRepresentable {
    @Binding var pkcanvasview: PKCanvasView
    let pktoolpicker = PKToolPicker()

    func makeUIView(context: Context) -> PKCanvasView {
        self.pkcanvasview.isOpaque = false
        self.pkcanvasview.backgroundColor = .clear
        self.pkcanvasview.overrideUserInterfaceStyle = .light
        self.pktoolpicker.addObserver(pkcanvasview)
        self.pktoolpicker.setVisible(true, forFirstResponder: pkcanvasview)
        self.pkcanvasview.becomeFirstResponder()
        return pkcanvasview
    }
    
    func updateUIView(_ uiView: PKCanvasView, context: Context) {
    }
}

SwiftSwift,SwiftUI

Posted by shi-n