将signatureView.signature保存为另一个视图的绑定变量

问题描述 投票:0回答:1

我将signatureView.signature保存到变量中并直接将其作为图像输出。

@State private var signatureImage: UIImage? 
@Binding var signature: String


SignatureCaptureView(signatureImage: $signatureImage) 
    .frame(height: 200) 
    .border(Color.gray, width: 1)


if let img = signatureImage {
        .Image(uiImage: img)
        .resizable()
        .scaledToFit()
        .frame(width: 300, height: 200)
        .background(Color.gray.opacity(0.2))
        .id(UUID())
} else {
    Text("Noch keine Unterschrift")
        .background(Color.gray.opacity(0.2))
        .frame(width: 300, height: 200)
}

但它没有显示最后一张图。所以如果我画两笔,它只显示第一笔。如果我画 3 条线,它只显示前两条线。所以它总是不显示最后一个。

SignatureCaptureView.swift:

import SwiftUI
import SwiftSignatureView


struct SignatureCaptureView: UIViewRepresentable {
    @Binding var signatureImage: UIImage?
    private var signatureView: SwiftSignatureView // Diese Eigenschaft hinzufügen
    var coordinator: Coordinator? // Füge ein Coordinator-Objekt hinzu

    func getCroppedSignature(from view: SwiftSignatureView) -> UIImage? {
        return view.getCroppedSignature()
    }

    init(signatureImage: Binding<UIImage?>) {
        self._signatureImage = signatureImage
        self.signatureView = SwiftSignatureView()
    }
    func makeUIView(context: Context) -> SwiftSignatureView {
        let signatureView = SwiftSignatureView()
        signatureView.delegate = context.coordinator
        return signatureView
    }

    func updateUIView(_ uiView: SwiftSignatureView, context: Context) {
    }

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    class Coordinator: NSObject, SwiftSignatureViewDelegate {
        var parent: SignatureCaptureView
        var signatureView: PencilKitSignatureView? // Speichere die SignatureView


        init(_ parent: SignatureCaptureView) {
            self.parent = parent
        }

        
        func updateUIView(_ view: ISignatureView) {
            if let signatureView = view as? PencilKitSignatureView {
                parent.signatureImage = signatureView.signature
                view.signature = signatureView.signature
            }
            
        }

        func swiftSignatureViewDidDrawGesture(_ view: ISignatureView, _ tap: UIGestureRecognizer) {
            //print("triggered: DrawGesture")
            if let signatureView = view as? PencilKitSignatureView {
                //parent.signatureImage = signatureView.signature
            }
        }

        func swiftSignatureViewDidDraw(_ view: ISignatureView) {
            print("triggered: DidDraw")
            if let signatureView = view as? PencilKitSignatureView {
                print("saving...")
               parent.signatureImage = signatureView.signature
            } else {
                print("View konnte nicht zu PencilKitSignatureView gecastet werden")
            }
        }






    }
}

我画了2条“线”

Image No 1

我画了最后一条线(1和2之间的3条线)

Image No 2

ios swift xcode signature
1个回答
0
投票

绘制完成后,只需调用更新函数即可:

func swiftSignatureViewDidDraw(_ view: ISignatureView) {
    updateUIView(_ view: view)
}

你可以移动这个块来更新功能,因为它已经部分完成了

func updateUIView(_ view: ISignatureView) {
    if let signatureView = view as? PencilKitSignatureView {
        parent.signatureImage = signatureView.signature
        //view.signature = signatureView.signature 
        //no need because signatureView already came from view, besides I think it is "let" parameter and will not allow you to assign.
    } else {
        print("View konnte nicht zu PencilKitSignatureView gecastet werden")
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.