質問編集履歴

2

自分の質問に語弊があった。

2020/09/07 05:49

投稿

Anoa
Anoa

スコア10

test CHANGED
File without changes
test CHANGED
@@ -1,214 +1,4 @@
1
- ```Swift
2
-
3
- import UIKit
4
-
5
-
6
-
7
- private let reuseIdentifier = "Cell"
8
-
9
-
10
-
11
- class CollectionViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
12
-
13
- var redCellIndex: Int = Int.random(in: 0..<12)
14
-
15
-
16
-
17
- @IBOutlet var collectionView: UICollectionView!
18
-
19
-
20
-
21
- @IBOutlet var collectionView2: UICollectionView!
22
-
23
-
24
-
25
- @IBOutlet var pointLabel:UILabel!
26
-
27
-
28
-
29
- @IBOutlet var timerLabel:UILabel!
30
-
31
-
32
-
33
- var point:Int=0
34
-
35
-
36
-
37
- var timer:Timer = Timer()
38
-
39
-
40
-
41
- var count:Float = 10
42
-
43
-
44
-
45
- var rand:Int=0
46
-
47
-
48
-
49
- var OnGame = false
50
-
51
-
52
-
53
- override func viewDidLoad() {
54
-
55
- super.viewDidLoad()
56
-
57
-
58
-
59
- OnGame = true
60
-
61
- self.collectionView.delegate = self
62
-
63
- self.collectionView.dataSource = self
64
-
65
-
66
-
67
- let layout = UICollectionViewFlowLayout()
68
-
69
- layout.sectionInset = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
70
-
71
- collectionView.collectionViewLayout = layout
72
-
73
-
74
-
75
- if !timer.isValid {
76
-
77
- timer = Timer.scheduledTimer(timeInterval:0.01,target: self,selector: #selector(self.down),userInfo:nil,repeats:true)
78
-
79
- }
80
-
81
-
82
-
83
- self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
84
-
85
- }
86
-
87
- @objc func changeColor() {
88
-
89
- let redCellIndex: Int = Int.random(in: 0..<12)
90
-
91
- collectionView.reloadData()
92
-
93
- }
94
-
95
- // 中略
96
-
97
-
98
-
99
- func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
100
-
101
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
102
-
103
-
104
-
105
- cell.backgroundColor = self.redCellIndex == indexPath.row ? .red : .black
106
-
107
-
108
-
109
- return cell
110
-
111
-
112
-
113
- }
114
-
115
-
116
-
117
- func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
118
-
119
- if indexPath.row == redCellIndex && OnGame{
120
-
121
- print("赤いセル")
122
-
123
- point = point+1
124
-
125
- timerLabel.text="Finish"
126
-
127
- point = point+0
128
-
129
- // pointLabel.text="GameOver"
130
-
131
- pointLabel.text = String(point)
132
-
133
- timerLabel.text="0"
134
-
135
- // pointLabel.invalidate()
136
-
137
- // pointLabel.text="0"
138
-
139
-
140
-
141
- } else {
142
-
143
- print("黒いセル")
144
-
145
- pointLabel.text = "GameOver"
146
-
147
- timer.invalidate()
148
-
149
- timerLabel.text = "0"
150
-
151
- OnGame = false
152
-
153
- // pointLabel.text="0"
154
-
155
- }
156
-
157
-
158
-
159
- }
160
-
161
-
162
-
163
- func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
164
-
165
- return 12
166
-
167
- }
168
-
169
-
170
-
171
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
172
-
173
- let horizontalSpace : CGFloat = 0.2
174
-
175
- let cellSize : CGFloat = self.view.bounds.width / 4 - horizontalSpace
176
-
177
- return CGSize(width: cellSize, height: cellSize)
178
-
179
- }
180
-
181
-
182
-
183
- @objc func down(){
184
-
185
- if count>=0{
186
-
187
- count = count - 0.01
188
-
189
- timerLabel.text = String(format: "%.2f",count)
190
-
191
- }else{
192
-
193
- if timer.isValid{
194
-
195
- timer.invalidate()
196
-
197
- timerLabel.text = "Finish"
198
-
199
- // point = point+0
200
-
201
- }
202
-
203
- }
204
-
205
- }
206
-
207
- }
208
-
209
-
210
-
211
- ```Main Story boardにCollection Viewを設置し、そこに何個かボタン(cell)を設置しました。そのcellを時間ごとに色を変えたいのですがどうすればよいのですか??
1
+ Main Story boardにCollection Viewを設置し、そこに何個かボタン(cell)を設置しました。そのcellを時間ごとに色を変えたいのですがどうすればよいのですか??
212
2
 
213
3
  例:6つのcellのうち5つは青1つは黄色で1秒ごとに変える
214
4
 

1

code内容の追加

2020/09/07 05:49

投稿

Anoa
Anoa

スコア10

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,214 @@
1
+ ```Swift
2
+
3
+ import UIKit
4
+
5
+
6
+
7
+ private let reuseIdentifier = "Cell"
8
+
9
+
10
+
11
+ class CollectionViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
12
+
13
+ var redCellIndex: Int = Int.random(in: 0..<12)
14
+
15
+
16
+
17
+ @IBOutlet var collectionView: UICollectionView!
18
+
19
+
20
+
21
+ @IBOutlet var collectionView2: UICollectionView!
22
+
23
+
24
+
25
+ @IBOutlet var pointLabel:UILabel!
26
+
27
+
28
+
29
+ @IBOutlet var timerLabel:UILabel!
30
+
31
+
32
+
33
+ var point:Int=0
34
+
35
+
36
+
37
+ var timer:Timer = Timer()
38
+
39
+
40
+
41
+ var count:Float = 10
42
+
43
+
44
+
45
+ var rand:Int=0
46
+
47
+
48
+
49
+ var OnGame = false
50
+
51
+
52
+
53
+ override func viewDidLoad() {
54
+
55
+ super.viewDidLoad()
56
+
57
+
58
+
59
+ OnGame = true
60
+
61
+ self.collectionView.delegate = self
62
+
63
+ self.collectionView.dataSource = self
64
+
65
+
66
+
67
+ let layout = UICollectionViewFlowLayout()
68
+
69
+ layout.sectionInset = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
70
+
71
+ collectionView.collectionViewLayout = layout
72
+
73
+
74
+
75
+ if !timer.isValid {
76
+
77
+ timer = Timer.scheduledTimer(timeInterval:0.01,target: self,selector: #selector(self.down),userInfo:nil,repeats:true)
78
+
79
+ }
80
+
81
+
82
+
83
+ self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
84
+
85
+ }
86
+
87
+ @objc func changeColor() {
88
+
89
+ let redCellIndex: Int = Int.random(in: 0..<12)
90
+
91
+ collectionView.reloadData()
92
+
93
+ }
94
+
95
+ // 中略
96
+
97
+
98
+
99
+ func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
100
+
101
+ let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
102
+
103
+
104
+
105
+ cell.backgroundColor = self.redCellIndex == indexPath.row ? .red : .black
106
+
107
+
108
+
109
+ return cell
110
+
111
+
112
+
113
+ }
114
+
115
+
116
+
117
+ func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
118
+
119
+ if indexPath.row == redCellIndex && OnGame{
120
+
121
+ print("赤いセル")
122
+
123
+ point = point+1
124
+
125
+ timerLabel.text="Finish"
126
+
127
+ point = point+0
128
+
129
+ // pointLabel.text="GameOver"
130
+
131
+ pointLabel.text = String(point)
132
+
133
+ timerLabel.text="0"
134
+
135
+ // pointLabel.invalidate()
136
+
137
+ // pointLabel.text="0"
138
+
139
+
140
+
141
+ } else {
142
+
143
+ print("黒いセル")
144
+
145
+ pointLabel.text = "GameOver"
146
+
147
+ timer.invalidate()
148
+
149
+ timerLabel.text = "0"
150
+
151
+ OnGame = false
152
+
153
+ // pointLabel.text="0"
154
+
155
+ }
156
+
157
+
158
+
159
+ }
160
+
161
+
162
+
163
+ func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
164
+
165
+ return 12
166
+
167
+ }
168
+
169
+
170
+
171
+ func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
172
+
173
+ let horizontalSpace : CGFloat = 0.2
174
+
175
+ let cellSize : CGFloat = self.view.bounds.width / 4 - horizontalSpace
176
+
177
+ return CGSize(width: cellSize, height: cellSize)
178
+
179
+ }
180
+
181
+
182
+
183
+ @objc func down(){
184
+
185
+ if count>=0{
186
+
187
+ count = count - 0.01
188
+
189
+ timerLabel.text = String(format: "%.2f",count)
190
+
191
+ }else{
192
+
193
+ if timer.isValid{
194
+
195
+ timer.invalidate()
196
+
197
+ timerLabel.text = "Finish"
198
+
199
+ // point = point+0
200
+
201
+ }
202
+
203
+ }
204
+
205
+ }
206
+
207
+ }
208
+
209
+
210
+
1
- Main Story boardにCollection Viewを設置し、そこに何個かボタン(cell)を設置しました。そのcellを時間ごとに色を変えたいのですがどうすればよいのですか??
211
+ ```Main Story boardにCollection Viewを設置し、そこに何個かボタン(cell)を設置しました。そのcellを時間ごとに色を変えたいのですがどうすればよいのですか??
2
212
 
3
213
  例:6つのcellのうち5つは青1つは黄色で1秒ごとに変える
4
214