回答編集履歴
2
コメントのもれ
test
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
`didSelectRow`を追加する必要があります。
|
2
|
+
|
3
|
+
|
4
|
+
|
1
5
|
```swift
|
2
6
|
|
3
7
|
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
|
1
追加
test
CHANGED
@@ -1,223 +1,229 @@
|
|
1
1
|
```swift
|
2
2
|
|
3
|
-
func pickerView(pickerView: UIPickerView
|
3
|
+
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
|
4
|
-
|
4
|
+
|
5
|
-
{
|
5
|
+
{
|
6
|
+
|
6
|
-
|
7
|
+
if component == 0 {
|
8
|
+
|
9
|
+
timeCountLabel.text = String(format: "%.1f", list1[row])
|
10
|
+
|
11
|
+
interval = list1[row]
|
12
|
+
|
13
|
+
}
|
14
|
+
|
15
|
+
}
|
16
|
+
|
17
|
+
```
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
全てのコードはこうなります;
|
22
|
+
|
23
|
+
```swift
|
24
|
+
|
25
|
+
import UIKit
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
class TimerViewController: UIViewController,UIPickerViewDelegate, UIPickerViewDataSource {
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
@IBOutlet weak var timeCountLabel: UILabel!
|
34
|
+
|
35
|
+
@IBOutlet weak var PickerView: UIPickerView!
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
var timer: Timer?
|
40
|
+
|
41
|
+
var interval = 0.0
|
42
|
+
|
43
|
+
var count: Int = 0
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
//カウントアップの間隔
|
48
|
+
|
49
|
+
let list1:[Double] = [0.5,1.0,1.5,2.0,2.5,3.0,3.5,4.0,4.5,5.0,5.5,6.0,7.5,8.0,8.5,9.0,9.5,10.0]
|
50
|
+
|
51
|
+
//カウントの上限
|
52
|
+
|
53
|
+
let list2:[Int] = [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]
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
override func viewDidLoad() {
|
58
|
+
|
59
|
+
super.viewDidLoad()
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
PickerView.delegate = self
|
64
|
+
|
65
|
+
PickerView.dataSource = self
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
PickerView.selectRow(0, inComponent: 0, animated: false)
|
70
|
+
|
71
|
+
PickerView.selectRow(1, inComponent: 1, animated: false)
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
// 表示を初期化
|
76
|
+
|
77
|
+
clear()
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
|
86
|
+
|
87
|
+
{
|
88
|
+
|
89
|
+
print(component)
|
90
|
+
|
91
|
+
if component == 0 {
|
92
|
+
|
93
|
+
timeCountLabel.text = String(format: "%.1f", list1[row])
|
94
|
+
|
95
|
+
interval = list1[row]
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
// ドラムロールの列数
|
104
|
+
|
105
|
+
func numberOfComponents(in pickerView: UIPickerView) -> Int {
|
106
|
+
|
107
|
+
return 2
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
// ドラムロールの行数
|
114
|
+
|
115
|
+
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
|
116
|
+
|
117
|
+
switch component {
|
118
|
+
|
119
|
+
case 0:
|
120
|
+
|
121
|
+
return list1.count
|
122
|
+
|
123
|
+
case 1:
|
124
|
+
|
125
|
+
return list2.count
|
126
|
+
|
127
|
+
default:
|
128
|
+
|
129
|
+
return 0
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
switch component {
|
146
|
+
|
147
|
+
case 0:
|
148
|
+
|
149
|
+
return String(format: "%.1f秒", list1[row])
|
150
|
+
|
151
|
+
case 1:
|
152
|
+
|
153
|
+
return String(format: "%.1f秒", Double(list2[row]))
|
154
|
+
|
155
|
+
default:
|
156
|
+
|
157
|
+
return "error"
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
@IBAction func start() {
|
166
|
+
|
167
|
+
// 起動中のタイマーを無効化
|
168
|
+
|
169
|
+
timer?.invalidate()
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
timer = Timer.scheduledTimer(withTimeInterval: self.interval, repeats: true) { (timer) in
|
174
|
+
|
175
|
+
|
176
|
+
|
7
|
-
print
|
177
|
+
print("counter")
|
178
|
+
|
8
|
-
|
179
|
+
self.count = self.count + 1
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
DispatchQueue.main.async {
|
184
|
+
|
9
|
-
|
185
|
+
self.timeCountLabel.text = String(self.count)
|
186
|
+
|
10
|
-
|
187
|
+
}
|
188
|
+
|
11
|
-
|
189
|
+
if self.count == Int(self.list2[3]) {
|
190
|
+
|
191
|
+
timer.invalidate()
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
@IBAction func stop() {
|
204
|
+
|
205
|
+
// 起動中のタイマーを無効化(ただし、count変数の中身とラベルの表示はそのまま)
|
206
|
+
|
207
|
+
timer?.invalidate()
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
@IBAction func clear() {
|
214
|
+
|
215
|
+
// 起動中のタイマーを無効化し、count変数とラベルの表示を初期化
|
216
|
+
|
217
|
+
timer?.invalidate()
|
218
|
+
|
219
|
+
count = 0
|
220
|
+
|
221
|
+
self.timeCountLabel.text = String(self.count)
|
222
|
+
|
223
|
+
}
|
12
224
|
|
13
225
|
}
|
14
226
|
|
227
|
+
|
228
|
+
|
15
229
|
```
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
全てのコードはこうなります;
|
20
|
-
|
21
|
-
```swift
|
22
|
-
|
23
|
-
import UIKit
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
class TimerViewController: UIViewController,UIPickerViewDelegate, UIPickerViewDataSource {
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
@IBOutlet weak var timeCountLabel: UILabel!
|
32
|
-
|
33
|
-
@IBOutlet weak var PickerView: UIPickerView!
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
var timer: Timer?
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
var count: Int = 0
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
//カウントアップの間隔
|
46
|
-
|
47
|
-
let list1:[Double] = [0.5,1.0,1.5,2.0,2.5,3.0,3.5,4.0,4.5,5.0,5.5,6.0,7.5,8.0,8.5,9.0,9.5,10.0]
|
48
|
-
|
49
|
-
//カウントの上限
|
50
|
-
|
51
|
-
let list2:[Int] = [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]
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
override func viewDidLoad() {
|
56
|
-
|
57
|
-
super.viewDidLoad()
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
PickerView.delegate = self
|
62
|
-
|
63
|
-
PickerView.dataSource = self
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
PickerView.selectRow(0, inComponent: 0, animated: false)
|
68
|
-
|
69
|
-
PickerView.selectRow(1, inComponent: 1, animated: false)
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
// 表示を初期化
|
74
|
-
|
75
|
-
clear()
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
}
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
|
84
|
-
|
85
|
-
{
|
86
|
-
|
87
|
-
print(component)
|
88
|
-
|
89
|
-
if component == 0 {
|
90
|
-
|
91
|
-
timeCountLabel.text = String(format: "%.1f", list1[row])
|
92
|
-
|
93
|
-
}
|
94
|
-
|
95
|
-
}
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
// ドラムロールの列数
|
100
|
-
|
101
|
-
func numberOfComponents(in pickerView: UIPickerView) -> Int {
|
102
|
-
|
103
|
-
return 2
|
104
|
-
|
105
|
-
}
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
// ドラムロールの行数
|
110
|
-
|
111
|
-
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
|
112
|
-
|
113
|
-
switch component {
|
114
|
-
|
115
|
-
case 0:
|
116
|
-
|
117
|
-
return list1.count
|
118
|
-
|
119
|
-
case 1:
|
120
|
-
|
121
|
-
return list2.count
|
122
|
-
|
123
|
-
default:
|
124
|
-
|
125
|
-
return 0
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
}
|
132
|
-
|
133
|
-
}
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
switch component {
|
142
|
-
|
143
|
-
case 0:
|
144
|
-
|
145
|
-
return String(format: "%.1f秒", list1[row])
|
146
|
-
|
147
|
-
case 1:
|
148
|
-
|
149
|
-
return String(format: "%.1f秒", Double(list2[row]))
|
150
|
-
|
151
|
-
default:
|
152
|
-
|
153
|
-
return "error"
|
154
|
-
|
155
|
-
}
|
156
|
-
|
157
|
-
}
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
@IBAction func start() {
|
162
|
-
|
163
|
-
// 起動中のタイマーを無効化
|
164
|
-
|
165
|
-
timer?.invalidate()
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
timer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { (timer) in
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
self.count = self.count + 1
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
DispatchQueue.main.async {
|
178
|
-
|
179
|
-
self.timeCountLabel.text = String(self.count)
|
180
|
-
|
181
|
-
}
|
182
|
-
|
183
|
-
if self.count == Int(self.list2[3]) {
|
184
|
-
|
185
|
-
timer.invalidate()
|
186
|
-
|
187
|
-
}
|
188
|
-
|
189
|
-
}
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
}
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
@IBAction func stop() {
|
198
|
-
|
199
|
-
// 起動中のタイマーを無効化(ただし、count変数の中身とラベルの表示はそのまま)
|
200
|
-
|
201
|
-
timer?.invalidate()
|
202
|
-
|
203
|
-
}
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
@IBAction func clear() {
|
208
|
-
|
209
|
-
// 起動中のタイマーを無効化し、count変数とラベルの表示を初期化
|
210
|
-
|
211
|
-
timer?.invalidate()
|
212
|
-
|
213
|
-
count = 0
|
214
|
-
|
215
|
-
self.timeCountLabel.text = String(self.count)
|
216
|
-
|
217
|
-
}
|
218
|
-
|
219
|
-
}
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
```
|