質問編集履歴

1

ご指摘通り<code>にコードを書きました。よろしくお願い致します。

2020/04/16 10:42

投稿

konokarakaua
konokarakaua

スコア6

test CHANGED
File without changes
test CHANGED
@@ -26,111 +26,113 @@
26
26
 
27
27
 
28
28
 
29
- ###[コード]
29
+ ```SwiftUI
30
+
31
+
30
32
 
31
33
  public struct Lock: View {
32
34
 
33
35
 
34
36
 
35
- var maxDigits: Int = 4
36
-
37
- var label = "Enter One Time Password"
38
-
39
-
40
-
41
- @State var pin: String = ""
42
-
43
- @State var showPin = false
44
-
45
- @State var isDisabled = false
46
-
47
-
48
-
49
-
50
-
51
- var handler: (String, (Bool) -> Void) -> Void
52
-
53
-
54
-
55
- public var body: some View {
56
-
57
- VStack(spacing: 30) {
58
-
59
- Text(label).font(.title)
60
-
61
- ZStack {
62
-
63
- pinDots
64
-
65
- backgroundField
66
-
67
- }
68
-
69
- showPinStack
70
-
71
- }
72
-
73
-
74
-
75
- }
76
-
77
-
78
-
79
- private var pinDots: some View {
80
-
81
- HStack {
82
-
83
- Spacer()
84
-
85
-
86
-
87
- ###//エラー箇所
88
-
89
- ForEach(0..<maxDigits) { index in
90
-
91
- Image(systemName: self.getImageName(at: index))
92
-
93
- .font(.system(size:30, weight:30, design: .default))
94
-
95
- Spacer()
96
-
97
- }
98
-
99
- }
100
-
101
- }
102
-
103
-
104
-
105
- private var backgroundField: some View {
106
-
107
- let boundPin = Binding<String>(get: { self.pin }, set: { newValue in
108
-
109
- self.pin = newValue
110
-
111
- self.submitPin()
112
-
113
- })
114
-
115
-
116
-
117
- return TextField("", text: boundPin, onCommit: submitPin)
118
-
119
-
120
-
121
- // Introspect library can used to make the textField become first resonder on appearing
122
-
123
- // if you decide to add the pod 'Introspect' and import it, comment #50 to #53 and uncomment #55 to #61
124
-
125
-
126
-
127
- .accentColor(.clear)
128
-
129
- .foregroundColor(.clear)
130
-
131
- .keyboardType(.numberPad)
132
-
133
- .disabled(isDisabled)
37
+ var maxDigits: Int = 4
38
+
39
+ var label = "Enter One Time Password"
40
+
41
+
42
+
43
+ @State var pin: String = ""
44
+
45
+ @State var showPin = false
46
+
47
+ @State var isDisabled = false
48
+
49
+
50
+
51
+ var handler: (String, (Bool) -> Void) -> Void
52
+
53
+
54
+
55
+ public var body: some View {
56
+
57
+ VStack(spacing: 30) {
58
+
59
+ Text(label).font(.title)
60
+
61
+ ZStack {
62
+
63
+ pinDots
64
+
65
+ backgroundField
66
+
67
+ }
68
+
69
+ showPinStack
70
+
71
+ }
72
+
73
+
74
+
75
+ }
76
+
77
+
78
+
79
+ private var pinDots: some View {
80
+
81
+ HStack {
82
+
83
+ Spacer()
84
+
85
+
86
+
87
+ //エラー箇所
88
+
89
+
90
+
91
+ ForEach(0..<maxDigits) { index in
92
+
93
+ Image(systemName: self.getImageName(at: index))
94
+
95
+ .font(.system(size:30, weight:30, design: .default))
96
+
97
+ Spacer()
98
+
99
+ }
100
+
101
+ }
102
+
103
+ }
104
+
105
+
106
+
107
+ private var backgroundField: some View {
108
+
109
+ let boundPin = Binding<String>(get: { self.pin }, set: { newValue in
110
+
111
+ self.pin = newValue
112
+
113
+ self.submitPin()
114
+
115
+ })
116
+
117
+
118
+
119
+ return TextField("", text: boundPin, onCommit: submitPin)
120
+
121
+
122
+
123
+ // Introspect library can used to make the textField become first resonder on appearing
124
+
125
+ // if you decide to add the pod 'Introspect' and import it, comment #50 to #53 and uncomment #55 to #61
126
+
127
+
128
+
129
+ .accentColor(.clear)
130
+
131
+ .foregroundColor(.clear)
132
+
133
+ .keyboardType(.numberPad)
134
+
135
+ .disabled(isDisabled)
134
136
 
135
137
 
136
138
 
@@ -148,129 +150,129 @@
148
150
 
149
151
  // }
150
152
 
151
- }
152
-
153
-
154
-
155
- private var showPinStack: some View {
156
-
157
- HStack {
158
-
159
- Spacer()
160
-
161
- if !pin.isEmpty {
162
-
163
- showPinButton
164
-
165
- }
166
-
167
- }
168
-
169
- .frame(height:30)
170
-
171
- .padding([.trailing])
172
-
173
- }
174
-
175
-
176
-
177
- private var showPinButton: some View {
178
-
179
- Button(action: {
180
-
181
- self.showPin.toggle()
182
-
183
- }, label: {
184
-
185
- self.showPin ?
186
-
187
- Image(systemName: "eye.slash.fill").foregroundColor(.primary) :
188
-
189
- Image(systemName: "eye.fill").foregroundColor(.primary)
190
-
191
- })
192
-
193
- }
194
-
195
-
196
-
197
- private func submitPin() {
198
-
199
- guard !pin.isEmpty else {
200
-
201
- showPin = false
202
-
203
- return
204
-
205
- }
206
-
207
-
208
-
209
- if pin.count == maxDigits {
210
-
211
- isDisabled = true
212
-
213
-
214
-
215
- handler(pin) { isSuccess in
216
-
217
- if isSuccess {
218
-
219
- print("pin matched, go to next page, no action to perfrom here")
220
-
221
- } else {
222
-
223
- pin = ""
224
-
225
- isDisabled = false
226
-
227
- print("this has to called after showing toast why is the failure")
228
-
229
- }
230
-
231
- }
232
-
233
- }
234
-
235
-
236
-
237
- // this code is never reached under normal circumstances. If the user pastes a text with count higher than the
238
-
239
- // max digits, we remove the additional characters and make a recursive call.
240
-
241
- if pin.count > maxDigits {
242
-
243
- pin = String(pin.prefix(maxDigits))
244
-
245
- submitPin()
246
-
247
- }
248
-
249
- }
250
-
251
-
252
-
253
- private func getImageName(at index: Int) -> String {
254
-
255
- if index >= self.pin.count {
256
-
257
- return "circle"
258
-
259
- }
260
-
261
-
262
-
263
- if self.showPin {
264
-
265
- return self.pin.digits[index].numberString + ".circle"
266
-
267
- }
268
-
269
-
270
-
271
- return "circle.fill"
272
-
273
- }
153
+ }
154
+
155
+
156
+
157
+ private var showPinStack: some View {
158
+
159
+ HStack {
160
+
161
+ Spacer()
162
+
163
+ if !pin.isEmpty {
164
+
165
+ showPinButton
166
+
167
+ }
168
+
169
+ }
170
+
171
+ .frame(height:30)
172
+
173
+ .padding([.trailing])
174
+
175
+ }
176
+
177
+
178
+
179
+ private var showPinButton: some View {
180
+
181
+ Button(action: {
182
+
183
+ self.showPin.toggle()
184
+
185
+ }, label: {
186
+
187
+ self.showPin ?
188
+
189
+ Image(systemName: "eye.slash.fill").foregroundColor(.primary) :
190
+
191
+ Image(systemName: "eye.fill").foregroundColor(.primary)
192
+
193
+ })
194
+
195
+ }
196
+
197
+
198
+
199
+ private func submitPin() {
200
+
201
+ guard !pin.isEmpty else {
202
+
203
+ showPin = false
204
+
205
+ return
206
+
207
+ }
208
+
209
+
210
+
211
+ if pin.count == maxDigits {
212
+
213
+ isDisabled = true
214
+
215
+
216
+
217
+ handler(pin) { isSuccess in
218
+
219
+ if isSuccess {
220
+
221
+ print("pin matched, go to next page, no action to perfrom here")
222
+
223
+ } else {
224
+
225
+ pin = ""
226
+
227
+ isDisabled = false
228
+
229
+ print("this has to called after showing toast why is the failure")
230
+
231
+ }
232
+
233
+ }
234
+
235
+ }
236
+
237
+
238
+
239
+ // this code is never reached under normal circumstances. If the user pastes a text with count higher than the
240
+
241
+ // max digits, we remove the additional characters and make a recursive call.
242
+
243
+ if pin.count > maxDigits {
244
+
245
+ pin = String(pin.prefix(maxDigits))
246
+
247
+ submitPin()
248
+
249
+ }
250
+
251
+ }
252
+
253
+
254
+
255
+ private func getImageName(at index: Int) -> String {
256
+
257
+ if index >= self.pin.count {
258
+
259
+ return "circle"
260
+
261
+ }
262
+
263
+
264
+
265
+ if self.showPin {
266
+
267
+ return self.pin.digits[index].numberString + ".circle"
268
+
269
+ }
270
+
271
+
272
+
273
+ return "circle.fill"
274
+
275
+ }
274
276
 
275
277
  }
276
278
 
@@ -280,27 +282,27 @@
280
282
 
281
283
 
282
284
 
283
- var digits: [Int] {
285
+ var digits: [Int] {
284
-
286
+
285
- var result = [Int]()
287
+ var result = [Int]()
286
-
287
-
288
-
288
+
289
+
290
+
289
- for char in self {
291
+ for char in self {
290
-
292
+
291
- if let number = Int(String(char)) {
293
+ if let number = Int(String(char)) {
292
-
294
+
293
- result.append(number)
295
+ result.append(number)
294
-
296
+
295
- }
297
+ }
296
-
298
+
297
- }
299
+ }
298
-
299
-
300
-
300
+
301
+
302
+
301
- return result
303
+ return result
302
-
304
+
303
- }
305
+ }
304
306
 
305
307
 
306
308
 
@@ -312,16 +314,18 @@
312
314
 
313
315
 
314
316
 
315
- var numberString: String {
317
+ var numberString: String {
316
-
317
-
318
-
318
+
319
+
320
+
319
- guard self < 10 else { return "0" }
321
+ guard self < 10 else { return "0" }
320
-
321
-
322
-
322
+
323
+
324
+
323
- return String(self)
325
+ return String(self)
324
-
326
+
325
- }
327
+ }
328
+
326
-
329
+ }
330
+
327
- }**ボールドテキスト**
331
+ ```