質問するログイン新規登録

質問編集履歴

1

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

2020/04/16 10:42

投稿

konokarakaua
konokarakaua

スコア6

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
- var maxDigits: Int = 4
19
+ var maxDigits: Int = 4
19
- var label = "Enter One Time Password"
20
+ var label = "Enter One Time Password"
20
21
 
21
- @State var pin: String = ""
22
+ @State var pin: String = ""
22
- @State var showPin = false
23
+ @State var showPin = false
23
- @State var isDisabled = false
24
+ @State var isDisabled = false
24
25
 
26
+ var handler: (String, (Bool) -> Void) -> Void
25
27
 
26
- var handler: (String, (Bool) -> Void) -> Void
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
- private var pinDots: some View {
44
+ //エラー箇所
41
- HStack {
42
- Spacer()
43
45
 
44
- ###//エラー箇所
45
- ForEach(0..<maxDigits) { index in
46
+ ForEach(0..<maxDigits) { index in
46
- Image(systemName: self.getImageName(at: index))
47
+ Image(systemName: self.getImageName(at: index))
47
- .font(.system(size:30, weight:30, design: .default))
48
+ .font(.system(size:30, weight:30, design: .default))
48
- Spacer()
49
+ Spacer()
49
- }
50
+ }
50
- }
51
+ }
51
- }
52
+ }
52
53
 
53
- private var backgroundField: some View {
54
+ private var backgroundField: some View {
54
- let boundPin = Binding<String>(get: { self.pin }, set: { newValue in
55
+ let boundPin = Binding<String>(get: { self.pin }, set: { newValue in
55
- self.pin = newValue
56
+ self.pin = newValue
56
- self.submitPin()
57
+ self.submitPin()
57
- })
58
+ })
58
59
 
59
- return TextField("", text: boundPin, onCommit: submitPin)
60
+ return TextField("", text: boundPin, onCommit: submitPin)
60
61
 
61
- // Introspect library can used to make the textField become first resonder on appearing
62
+ // Introspect library can used to make the textField become first resonder on appearing
62
- // if you decide to add the pod 'Introspect' and import it, comment #50 to #53 and uncomment #55 to #61
63
+ // if you decide to add the pod 'Introspect' and import it, comment #50 to #53 and uncomment #55 to #61
63
64
 
64
- .accentColor(.clear)
65
+ .accentColor(.clear)
65
- .foregroundColor(.clear)
66
+ .foregroundColor(.clear)
66
- .keyboardType(.numberPad)
67
+ .keyboardType(.numberPad)
67
- .disabled(isDisabled)
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
- private var showPinStack: some View {
79
+ private var showPinStack: some View {
79
- HStack {
80
+ HStack {
80
- Spacer()
81
+ Spacer()
81
- if !pin.isEmpty {
82
+ if !pin.isEmpty {
82
- showPinButton
83
+ showPinButton
83
- }
84
+ }
84
- }
85
+ }
85
- .frame(height:30)
86
+ .frame(height:30)
86
- .padding([.trailing])
87
+ .padding([.trailing])
87
- }
88
+ }
88
89
 
89
- private var showPinButton: some View {
90
+ private var showPinButton: some View {
90
- Button(action: {
91
+ Button(action: {
91
- self.showPin.toggle()
92
+ self.showPin.toggle()
92
- }, label: {
93
+ }, label: {
93
- self.showPin ?
94
+ self.showPin ?
94
- Image(systemName: "eye.slash.fill").foregroundColor(.primary) :
95
+ Image(systemName: "eye.slash.fill").foregroundColor(.primary) :
95
- Image(systemName: "eye.fill").foregroundColor(.primary)
96
+ Image(systemName: "eye.fill").foregroundColor(.primary)
96
- })
97
+ })
97
- }
98
+ }
98
99
 
99
- private func submitPin() {
100
+ private func submitPin() {
100
- guard !pin.isEmpty else {
101
+ guard !pin.isEmpty else {
101
- showPin = false
102
+ showPin = false
102
- return
103
+ return
103
- }
104
+ }
104
105
 
105
- if pin.count == maxDigits {
106
+ if pin.count == maxDigits {
106
- isDisabled = true
107
+ isDisabled = true
107
108
 
108
- handler(pin) { isSuccess in
109
+ handler(pin) { isSuccess in
109
- if isSuccess {
110
+ if isSuccess {
110
- print("pin matched, go to next page, no action to perfrom here")
111
+ print("pin matched, go to next page, no action to perfrom here")
111
- } else {
112
+ } else {
112
- pin = ""
113
+ pin = ""
113
- isDisabled = false
114
+ isDisabled = false
114
- print("this has to called after showing toast why is the failure")
115
+ print("this has to called after showing toast why is the failure")
115
- }
116
+ }
116
- }
117
+ }
117
- }
118
+ }
118
119
 
119
- // this code is never reached under normal circumstances. If the user pastes a text with count higher than the
120
+ // this code is never reached under normal circumstances. If the user pastes a text with count higher than the
120
- // max digits, we remove the additional characters and make a recursive call.
121
+ // max digits, we remove the additional characters and make a recursive call.
121
- if pin.count > maxDigits {
122
+ if pin.count > maxDigits {
122
- pin = String(pin.prefix(maxDigits))
123
+ pin = String(pin.prefix(maxDigits))
123
- submitPin()
124
+ submitPin()
124
- }
125
+ }
125
- }
126
+ }
126
127
 
127
- private func getImageName(at index: Int) -> String {
128
+ private func getImageName(at index: Int) -> String {
128
- if index >= self.pin.count {
129
+ if index >= self.pin.count {
129
- return "circle"
130
+ return "circle"
130
- }
131
+ }
131
132
 
132
- if self.showPin {
133
+ if self.showPin {
133
- return self.pin.digits[index].numberString + ".circle"
134
+ return self.pin.digits[index].numberString + ".circle"
134
- }
135
+ }
135
136
 
136
- return "circle.fill"
137
+ return "circle.fill"
137
- }
138
138
  }
139
+ }
139
140
 
140
141
  extension String {
141
142
 
142
- var digits: [Int] {
143
+ var digits: [Int] {
143
- var result = [Int]()
144
+ var result = [Int]()
144
145
 
145
- for char in self {
146
+ for char in self {
146
- if let number = Int(String(char)) {
147
+ if let number = Int(String(char)) {
147
- result.append(number)
148
+ result.append(number)
148
- }
149
+ }
149
- }
150
+ }
150
151
 
151
- return result
152
+ return result
152
- }
153
+ }
153
154
 
154
155
  }
155
156
 
156
157
  extension Int {
157
158
 
158
- var numberString: String {
159
+ var numberString: String {
159
160
 
160
- guard self < 10 else { return "0" }
161
+ guard self < 10 else { return "0" }
161
162
 
162
- return String(self)
163
+ return String(self)
163
- }
164
+ }
165
+ }
164
- }**ボールドテキスト**
166
+ ```