Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: SwiftUI Compatibility #51

Open
dannydenovi opened this issue Nov 28, 2020 · 4 comments
Open

Question: SwiftUI Compatibility #51

dannydenovi opened this issue Nov 28, 2020 · 4 comments

Comments

@dannydenovi
Copy link

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

@towcar
Copy link

towcar commented Feb 11, 2021

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
Copy link

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

@renevdkooi
Copy link

renevdkooi commented Oct 4, 2021

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants