美文网首页
swiftUI PencilKit的简单使用

swiftUI PencilKit的简单使用

作者: 王勋才 | 来源:发表于2021-12-05 20:38 被阅读0次
    
    //  PencilView.swift
    //  PencilDraw
    //  Created by wangxuncai on 2021/12/5.
    
    
    import SwiftUI
    import PencilKit
    
    
    
    struct PencilView: UIViewRepresentable {
       
        @Binding var canvas:PKCanvasView
    
        let toolPicker = PKToolPicker()
        func makeCoordinator() -> Coordinator {
            return Coordinator(parent: self)
        }
        func makeUIView(context: Context) -> PKCanvasView {
        
            
            toolPicker.setVisible(true, forFirstResponder:canvas )
            toolPicker.addObserver(canvas)
            canvas.backgroundColor = .clear
            canvas.delegate = context.coordinator
            canvas.becomeFirstResponder()
        
            return canvas
        }
        func updateUIView(_ uiView: PKCanvasView, context: Context) {
         
        }
        class Coordinator:NSObject,PKCanvasViewDelegate, PKToolPickerObserver{
            var parent:PencilView
            init(parent:PencilView){
                self.parent = parent
            }
        }
    }
    
    //使用
    struct CanvasBoard: View {
        @State var canvas = PKCanvasView()
        @Binding var image:UIImage
        var body: some View {
            PencilView(canvas: $canvas)
                .background(Image(uiImage: image).resizable())
        }
        
    }
    
    苹果应用商店|搜|王勋才|有我全部作品
    

    相关文章

      网友评论

          本文标题:swiftUI PencilKit的简单使用

          本文链接:https://www.haomeiwen.com/subject/lwdwxrtx.html