質問編集履歴
1
ご指摘通り<code>にコードを書きました。よろしくお願い致します。
title
CHANGED
|
File without changes
|
body
CHANGED
|
@@ -12,59 +12,60 @@
|
|
|
12
12
|
教えてください。よろしくお願い致します。
|
|
13
13
|
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
```SwiftUI
|
|
16
|
+
|
|
16
17
|
public struct Lock: View {
|
|
17
18
|
|
|
18
|
-
|
|
19
|
+
var maxDigits: Int = 4
|
|
19
|
-
|
|
20
|
+
var label = "Enter One Time Password"
|
|
20
21
|
|
|
21
|
-
|
|
22
|
+
@State var pin: String = ""
|
|
22
|
-
|
|
23
|
+
@State var showPin = false
|
|
23
|
-
|
|
24
|
+
@State var isDisabled = false
|
|
24
25
|
|
|
26
|
+
var handler: (String, (Bool) -> Void) -> Void
|
|
25
27
|
|
|
26
|
-
|
|
28
|
+
public var body: some View {
|
|
29
|
+
VStack(spacing: 30) {
|
|
30
|
+
Text(label).font(.title)
|
|
31
|
+
ZStack {
|
|
32
|
+
pinDots
|
|
33
|
+
backgroundField
|
|
34
|
+
}
|
|
35
|
+
showPinStack
|
|
36
|
+
}
|
|
27
37
|
|
|
28
|
-
public var body: some View {
|
|
29
|
-
VStack(spacing: 30) {
|
|
30
|
-
Text(label).font(.title)
|
|
31
|
-
ZStack {
|
|
32
|
-
pinDots
|
|
33
|
-
backgroundField
|
|
34
|
-
|
|
38
|
+
}
|
|
35
|
-
showPinStack
|
|
36
|
-
}
|
|
37
39
|
|
|
40
|
+
private var pinDots: some View {
|
|
38
|
-
|
|
41
|
+
HStack {
|
|
42
|
+
Spacer()
|
|
39
43
|
|
|
40
|
-
|
|
44
|
+
//エラー箇所
|
|
41
|
-
HStack {
|
|
42
|
-
Spacer()
|
|
43
45
|
|
|
44
|
-
###//エラー箇所
|
|
45
|
-
|
|
46
|
+
ForEach(0..<maxDigits) { index in
|
|
46
|
-
|
|
47
|
+
Image(systemName: self.getImageName(at: index))
|
|
47
|
-
|
|
48
|
+
.font(.system(size:30, weight:30, design: .default))
|
|
48
|
-
|
|
49
|
+
Spacer()
|
|
49
|
-
|
|
50
|
+
}
|
|
50
|
-
|
|
51
|
+
}
|
|
51
|
-
|
|
52
|
+
}
|
|
52
53
|
|
|
53
|
-
|
|
54
|
+
private var backgroundField: some View {
|
|
54
|
-
|
|
55
|
+
let boundPin = Binding<String>(get: { self.pin }, set: { newValue in
|
|
55
|
-
|
|
56
|
+
self.pin = newValue
|
|
56
|
-
|
|
57
|
+
self.submitPin()
|
|
57
|
-
|
|
58
|
+
})
|
|
58
59
|
|
|
59
|
-
|
|
60
|
+
return TextField("", text: boundPin, onCommit: submitPin)
|
|
60
61
|
|
|
61
|
-
|
|
62
|
+
// Introspect library can used to make the textField become first resonder on appearing
|
|
62
|
-
|
|
63
|
+
// if you decide to add the pod 'Introspect' and import it, comment #50 to #53 and uncomment #55 to #61
|
|
63
64
|
|
|
64
|
-
|
|
65
|
+
.accentColor(.clear)
|
|
65
|
-
|
|
66
|
+
.foregroundColor(.clear)
|
|
66
|
-
|
|
67
|
+
.keyboardType(.numberPad)
|
|
67
|
-
|
|
68
|
+
.disabled(isDisabled)
|
|
68
69
|
|
|
69
70
|
// .introspectTextField { textField in
|
|
70
71
|
// textField.tintColor = .clear
|
|
@@ -73,92 +74,93 @@
|
|
|
73
74
|
// textField.becomeFirstResponder()
|
|
74
75
|
// textField.isEnabled = !self.isDisabled
|
|
75
76
|
// }
|
|
76
|
-
|
|
77
|
+
}
|
|
77
78
|
|
|
78
|
-
|
|
79
|
+
private var showPinStack: some View {
|
|
79
|
-
|
|
80
|
+
HStack {
|
|
80
|
-
|
|
81
|
+
Spacer()
|
|
81
|
-
|
|
82
|
+
if !pin.isEmpty {
|
|
82
|
-
|
|
83
|
+
showPinButton
|
|
83
|
-
|
|
84
|
+
}
|
|
84
|
-
|
|
85
|
+
}
|
|
85
|
-
|
|
86
|
+
.frame(height:30)
|
|
86
|
-
|
|
87
|
+
.padding([.trailing])
|
|
87
|
-
|
|
88
|
+
}
|
|
88
89
|
|
|
89
|
-
|
|
90
|
+
private var showPinButton: some View {
|
|
90
|
-
|
|
91
|
+
Button(action: {
|
|
91
|
-
|
|
92
|
+
self.showPin.toggle()
|
|
92
|
-
|
|
93
|
+
}, label: {
|
|
93
|
-
|
|
94
|
+
self.showPin ?
|
|
94
|
-
|
|
95
|
+
Image(systemName: "eye.slash.fill").foregroundColor(.primary) :
|
|
95
|
-
|
|
96
|
+
Image(systemName: "eye.fill").foregroundColor(.primary)
|
|
96
|
-
|
|
97
|
+
})
|
|
97
|
-
|
|
98
|
+
}
|
|
98
99
|
|
|
99
|
-
|
|
100
|
+
private func submitPin() {
|
|
100
|
-
|
|
101
|
+
guard !pin.isEmpty else {
|
|
101
|
-
|
|
102
|
+
showPin = false
|
|
102
|
-
|
|
103
|
+
return
|
|
103
|
-
|
|
104
|
+
}
|
|
104
105
|
|
|
105
|
-
|
|
106
|
+
if pin.count == maxDigits {
|
|
106
|
-
|
|
107
|
+
isDisabled = true
|
|
107
108
|
|
|
108
|
-
|
|
109
|
+
handler(pin) { isSuccess in
|
|
109
|
-
|
|
110
|
+
if isSuccess {
|
|
110
|
-
|
|
111
|
+
print("pin matched, go to next page, no action to perfrom here")
|
|
111
|
-
|
|
112
|
+
} else {
|
|
112
|
-
|
|
113
|
+
pin = ""
|
|
113
|
-
|
|
114
|
+
isDisabled = false
|
|
114
|
-
|
|
115
|
+
print("this has to called after showing toast why is the failure")
|
|
115
|
-
|
|
116
|
+
}
|
|
116
|
-
|
|
117
|
+
}
|
|
117
|
-
|
|
118
|
+
}
|
|
118
119
|
|
|
119
|
-
|
|
120
|
+
// this code is never reached under normal circumstances. If the user pastes a text with count higher than the
|
|
120
|
-
|
|
121
|
+
// max digits, we remove the additional characters and make a recursive call.
|
|
121
|
-
|
|
122
|
+
if pin.count > maxDigits {
|
|
122
|
-
|
|
123
|
+
pin = String(pin.prefix(maxDigits))
|
|
123
|
-
|
|
124
|
+
submitPin()
|
|
124
|
-
|
|
125
|
+
}
|
|
125
|
-
|
|
126
|
+
}
|
|
126
127
|
|
|
127
|
-
|
|
128
|
+
private func getImageName(at index: Int) -> String {
|
|
128
|
-
|
|
129
|
+
if index >= self.pin.count {
|
|
129
|
-
|
|
130
|
+
return "circle"
|
|
130
|
-
|
|
131
|
+
}
|
|
131
132
|
|
|
132
|
-
|
|
133
|
+
if self.showPin {
|
|
133
|
-
|
|
134
|
+
return self.pin.digits[index].numberString + ".circle"
|
|
134
|
-
|
|
135
|
+
}
|
|
135
136
|
|
|
136
|
-
|
|
137
|
+
return "circle.fill"
|
|
137
|
-
}
|
|
138
138
|
}
|
|
139
|
+
}
|
|
139
140
|
|
|
140
141
|
extension String {
|
|
141
142
|
|
|
142
|
-
|
|
143
|
+
var digits: [Int] {
|
|
143
|
-
|
|
144
|
+
var result = [Int]()
|
|
144
145
|
|
|
145
|
-
|
|
146
|
+
for char in self {
|
|
146
|
-
|
|
147
|
+
if let number = Int(String(char)) {
|
|
147
|
-
|
|
148
|
+
result.append(number)
|
|
148
|
-
|
|
149
|
+
}
|
|
149
|
-
|
|
150
|
+
}
|
|
150
151
|
|
|
151
|
-
|
|
152
|
+
return result
|
|
152
|
-
|
|
153
|
+
}
|
|
153
154
|
|
|
154
155
|
}
|
|
155
156
|
|
|
156
157
|
extension Int {
|
|
157
158
|
|
|
158
|
-
|
|
159
|
+
var numberString: String {
|
|
159
160
|
|
|
160
|
-
|
|
161
|
+
guard self < 10 else { return "0" }
|
|
161
162
|
|
|
162
|
-
|
|
163
|
+
return String(self)
|
|
163
|
-
|
|
164
|
+
}
|
|
165
|
+
}
|
|
164
|
-
|
|
166
|
+
```
|