-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
SignatureView.swift
99 lines (90 loc) · 3.37 KB
/
SignatureView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// This source file is part of the CardinalKit open-source project
//
// SPDX-FileCopyrightText: 2022 Stanford University and the project authors (see CONTRIBUTORS.md)
//
// SPDX-License-Identifier: MIT
//
import PencilKit
import SwiftUI
import Views
/// A ``SignatureView`` enables the collection of signatures using a view that allows freeform signatures using a finger and the Apple Pencil.
///
/// Use SwiftUI `Bindings` to obtain information like the content of the signature and if the user is currently signing:
/// ```
/// @State var signature = PKDrawing()
/// @State var isSigning = false
///
///
/// SignatureView(
/// signature: $signature,
/// isSigning: $isSigning,
/// name: name
/// )
/// ```
public struct SignatureView: View {
@Environment(\.undoManager) private var undoManager
@Binding private var signature: PKDrawing
@Binding private var isSigning: Bool
@State private var canUndo = false
private let name: PersonNameComponents
private let lineOffset: CGFloat
public var body: some View {
VStack {
ZStack(alignment: .bottomLeading) {
Color(.secondarySystemBackground)
Rectangle()
.fill(.secondary)
.frame(maxWidth: .infinity, maxHeight: 1)
.padding(.horizontal, 20)
.padding(.bottom, lineOffset)
Text("X")
.font(.title2)
.foregroundColor(.secondary)
.padding(.horizontal, 20)
.padding(.bottom, lineOffset + 2)
Text(name.formatted(.name(style: .long)))
.font(.subheadline)
.foregroundColor(.secondary)
.padding(.horizontal, 20)
.padding(.bottom, lineOffset - 18)
CanvasView(drawing: $signature, isDrawing: $isSigning, showToolPicker: .constant(false))
}
.frame(height: 120)
Button(String(localized: "SIGNATURE_VIEW_UNDO", bundle: .module)) {
undoManager?.undo()
canUndo = undoManager?.canUndo ?? false
}
.disabled(!canUndo)
}
.onChange(of: isSigning) { _ in
Task { @MainActor in
canUndo = undoManager?.canUndo ?? false
}
}
.transition(.opacity)
.animation(.easeInOut, value: canUndo)
}
/// Creates a new instance of an ``SignatureView``.
/// - Parameters:
/// - signature: A `Binding` containing the current signature as an `PKDrawing`.
/// - isSigning: A `Binding` indicating if the user is currently signing.
/// - name: The name that is deplayed under the signature line.
/// - lineOffset: Defines the distance of the signature line from the bottom of the view. The default value is 30.
init(
signature: Binding<PKDrawing> = .constant(PKDrawing()),
isSigning: Binding<Bool> = .constant(false),
name: PersonNameComponents = PersonNameComponents(),
lineOffset: CGFloat = 30
) {
self._signature = signature
self._isSigning = isSigning
self.name = name
self.lineOffset = lineOffset
}
}
struct SignatureView_Previews: PreviewProvider {
static var previews: some View {
SignatureView()
}
}