alankarmisra / SwiftSignatureView

A lightweight, fast and customizable option for capturing fluid, variable-stroke-width signatures within your app.
MIT License
320 stars 93 forks source link

Question: SwiftUI Compatibility #51

Open dannydenovi opened 3 years ago

dannydenovi commented 3 years ago

Hello there! How can I use this framework in SwiftUI?

towcar commented 3 years ago

This should work for you

struct SignatureView: UIViewRepresentable{

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

    }

    func makeUIView(context: Context) -> SwiftSignatureView{

        return SwiftSignatureView()
    }
}

Add this into it's own file, and you can use it in SwiftUI with SignatureView()

To use various functions, add binding bool variables here, and in updateUIView you can check if they are true, then toggle them back to false.

pura-agency commented 3 years ago

Can you do some examples of how to use functions because I'm getting errors

renevdkooi commented 2 years ago

struct SignatureView: UIViewRepresentable {

@Environment(\.presentationMode) private var presentationMode
let sv: SwiftSignatureView = SwiftSignatureView()

func makeUIView(context: Context) -> SwiftSignatureView{

    sv.delegate = context.coordinator
    return sv;
}

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

}

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

final class Coordinator: NSObject, SwiftSignatureViewDelegate, UINavigationControllerDelegate {

    var parent: SignatureView

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

    func swiftSignatureViewDidDrawGesture(_ view: ISignatureView, _ tap: UIGestureRecognizer) {
        debugPrint("swiftSignatureViewDidDraw")
    }

    func swiftSignatureViewDidDraw(_ view: ISignatureView) {
        debugPrint("swiftSignatureViewDidDraw")
    }
}

}

twodayslate commented 2 years ago

Here is a working example that support undo, redo, and clear.

//
//  SwiftUISignatureView.swift
//  SwiftSignatureView
//
//  Created by Zachary Gorak on 4/27/22.
//
//  SwiftSignatureView is available under the MIT license. See the LICENSE file for more info.

#if canImport(SwiftUI)
import SwiftUI
import SwiftSignatureView

@available(iOS 13.0, *)
struct SwiftUISignatureView: UIViewRepresentable {
    var drawingFinished: ((ISignatureView)->Void)? = nil
    @Binding var clear: Bool
    @Binding var undo: Bool
    @Binding var redo: Bool

    init(clear: Binding<Bool>? = nil, undo: Binding<Bool>? = nil, redo: Binding<Bool>? = nil, swiftSignatureViewDidDraw: ((ISignatureView)->Void)? = nil) {
        self.drawingFinished = swiftSignatureViewDidDraw
        self._clear = clear ?? Binding.constant(false)
        self._undo = undo ?? Binding.constant(false)
        self._redo = redo ?? Binding.constant(false)
    }

    func makeUIView(context: Context) -> SwiftSignatureView {
        let ssv = SwiftSignatureView()
        ssv.setContentHuggingPriority(.required, for: .horizontal)
        ssv.setContentHuggingPriority(.required, for: .vertical)
        ssv.delegate = context.coordinator
        _ = clear
        _ = undo
        _ = redo
        return ssv
    }

    func updateUIView(_ uiView: SwiftSignatureView, context: Context) {
        // no-op
        DispatchQueue.main.async {
            if clear {
                uiView.clear()
                clear.toggle()
            }

            if undo {
                uiView.undo()
                undo.toggle()
            }

            if redo {
                uiView.redo()
                redo.toggle()
            }
            drawingFinished?(uiView)
        }
    }

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

    class Coordinator: SwiftSignatureViewDelegate {
        var parent: SwiftUISignatureView

        init(_ parent: SwiftUISignatureView) {
            self.parent = parent
        }
        func swiftSignatureViewDidDrawGesture(_ view: ISignatureView, _ tap: UIGestureRecognizer) {
            parent.drawingFinished?(view)
        }

        func swiftSignatureViewDidDraw(_ view: ISignatureView) {
            parent.drawingFinished?(view)
        }
    }
}

@available(iOS 13.0, *)
struct SwiftUISignatureViewTester: View {
    @State var clear: Bool = false
    @State var redo: Bool = false
    @State var undo: Bool = false
    @State var croppedImage: UIImage? = nil

    var body: some View {
        VStack {
            HStack {
                Button("Undo") {
                    undo = true
                }
                Button("Redo") {
                    redo = true
                }
                Spacer()
                Button("Clear") {
                    clear = true
                }
            }
            .padding()

            SwiftUISignatureView(clear: $clear, undo: $undo, redo: $redo) { signature in
                withAnimation {
                    croppedImage = signature.getCroppedSignature()
                }
            }
            .frame(height: 200)
            .border(Color.accentColor, width: 1)

            if let croppedImage = croppedImage {
                Image(uiImage: croppedImage)
                    .border(.red, width: 1)
            }

        }
    }
}

#if DEBUG
@available(iOS 13.0, *)
struct SwiftUISignatureView_Preview: PreviewProvider {

    static var previews: some View {
        Group {
            VStack {
                SwiftUISignatureViewTester()
            }
        }
        .previewLayout(.sizeThatFits)
    }
}
#endif
#endif

I would like to submit this as a PR but I'm having trouble with the Pods and getting it into the project.