質問編集履歴
2
自分の質問に語弊があった。
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,109 +1,4 @@
|
|
1
|
-
```Swift
|
2
|
-
import UIKit
|
3
|
-
|
4
|
-
private let reuseIdentifier = "Cell"
|
5
|
-
|
6
|
-
class CollectionViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
|
7
|
-
var redCellIndex: Int = Int.random(in: 0..<12)
|
8
|
-
|
9
|
-
@IBOutlet var collectionView: UICollectionView!
|
10
|
-
|
11
|
-
@IBOutlet var collectionView2: UICollectionView!
|
12
|
-
|
13
|
-
@IBOutlet var pointLabel:UILabel!
|
14
|
-
|
15
|
-
@IBOutlet var timerLabel:UILabel!
|
16
|
-
|
17
|
-
var point:Int=0
|
18
|
-
|
19
|
-
var timer:Timer = Timer()
|
20
|
-
|
21
|
-
var count:Float = 10
|
22
|
-
|
23
|
-
var rand:Int=0
|
24
|
-
|
25
|
-
var OnGame = false
|
26
|
-
|
27
|
-
override func viewDidLoad() {
|
28
|
-
super.viewDidLoad()
|
29
|
-
|
30
|
-
OnGame = true
|
31
|
-
self.collectionView.delegate = self
|
32
|
-
self.collectionView.dataSource = self
|
33
|
-
|
34
|
-
let layout = UICollectionViewFlowLayout()
|
35
|
-
layout.sectionInset = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
|
36
|
-
collectionView.collectionViewLayout = layout
|
37
|
-
|
38
|
-
if !timer.isValid {
|
39
|
-
timer = Timer.scheduledTimer(timeInterval:0.01,target: self,selector: #selector(self.down),userInfo:nil,repeats:true)
|
40
|
-
}
|
41
|
-
|
42
|
-
self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
|
43
|
-
}
|
44
|
-
@objc func changeColor() {
|
45
|
-
let redCellIndex: Int = Int.random(in: 0..<12)
|
46
|
-
collectionView.reloadData()
|
47
|
-
}
|
48
|
-
// 中略
|
49
|
-
|
50
|
-
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
51
|
-
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
|
52
|
-
|
53
|
-
cell.backgroundColor = self.redCellIndex == indexPath.row ? .red : .black
|
54
|
-
|
55
|
-
return cell
|
56
|
-
|
57
|
-
}
|
58
|
-
|
59
|
-
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
60
|
-
if indexPath.row == redCellIndex && OnGame{
|
61
|
-
print("赤いセル")
|
62
|
-
point = point+1
|
63
|
-
timerLabel.text="Finish"
|
64
|
-
point = point+0
|
65
|
-
// pointLabel.text="GameOver"
|
66
|
-
pointLabel.text = String(point)
|
67
|
-
timerLabel.text="0"
|
68
|
-
// pointLabel.invalidate()
|
69
|
-
// pointLabel.text="0"
|
70
|
-
|
71
|
-
} else {
|
72
|
-
print("黒いセル")
|
73
|
-
pointLabel.text = "GameOver"
|
74
|
-
timer.invalidate()
|
75
|
-
timerLabel.text = "0"
|
76
|
-
OnGame = false
|
77
|
-
// pointLabel.text="0"
|
78
|
-
}
|
79
|
-
|
80
|
-
}
|
81
|
-
|
82
|
-
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
83
|
-
return 12
|
84
|
-
}
|
85
|
-
|
86
|
-
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
|
87
|
-
let horizontalSpace : CGFloat = 0.2
|
88
|
-
let cellSize : CGFloat = self.view.bounds.width / 4 - horizontalSpace
|
89
|
-
return CGSize(width: cellSize, height: cellSize)
|
90
|
-
}
|
91
|
-
|
92
|
-
@objc func down(){
|
93
|
-
if count>=0{
|
94
|
-
count = count - 0.01
|
95
|
-
timerLabel.text = String(format: "%.2f",count)
|
96
|
-
}else{
|
97
|
-
if timer.isValid{
|
98
|
-
timer.invalidate()
|
99
|
-
timerLabel.text = "Finish"
|
100
|
-
// point = point+0
|
101
|
-
}
|
102
|
-
}
|
103
|
-
}
|
104
|
-
}
|
105
|
-
|
106
|
-
|
1
|
+
Main Story boardにCollection Viewを設置し、そこに何個かボタン(cell)を設置しました。そのcellを時間ごとに色を変えたいのですがどうすればよいのですか??
|
107
2
|
例:6つのcellのうち5つは青1つは黄色で1秒ごとに変える
|
108
3
|
みたいな感じです。
|
109
4
|
これはタイマーとくくりつけてやるのですか?(?)
|
1
code内容の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,4 +1,109 @@
|
|
1
|
+
```Swift
|
2
|
+
import UIKit
|
3
|
+
|
4
|
+
private let reuseIdentifier = "Cell"
|
5
|
+
|
6
|
+
class CollectionViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
|
7
|
+
var redCellIndex: Int = Int.random(in: 0..<12)
|
8
|
+
|
9
|
+
@IBOutlet var collectionView: UICollectionView!
|
10
|
+
|
11
|
+
@IBOutlet var collectionView2: UICollectionView!
|
12
|
+
|
13
|
+
@IBOutlet var pointLabel:UILabel!
|
14
|
+
|
15
|
+
@IBOutlet var timerLabel:UILabel!
|
16
|
+
|
17
|
+
var point:Int=0
|
18
|
+
|
19
|
+
var timer:Timer = Timer()
|
20
|
+
|
21
|
+
var count:Float = 10
|
22
|
+
|
23
|
+
var rand:Int=0
|
24
|
+
|
25
|
+
var OnGame = false
|
26
|
+
|
27
|
+
override func viewDidLoad() {
|
28
|
+
super.viewDidLoad()
|
29
|
+
|
30
|
+
OnGame = true
|
31
|
+
self.collectionView.delegate = self
|
32
|
+
self.collectionView.dataSource = self
|
33
|
+
|
34
|
+
let layout = UICollectionViewFlowLayout()
|
35
|
+
layout.sectionInset = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
|
36
|
+
collectionView.collectionViewLayout = layout
|
37
|
+
|
38
|
+
if !timer.isValid {
|
39
|
+
timer = Timer.scheduledTimer(timeInterval:0.01,target: self,selector: #selector(self.down),userInfo:nil,repeats:true)
|
40
|
+
}
|
41
|
+
|
42
|
+
self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
|
43
|
+
}
|
44
|
+
@objc func changeColor() {
|
45
|
+
let redCellIndex: Int = Int.random(in: 0..<12)
|
46
|
+
collectionView.reloadData()
|
47
|
+
}
|
48
|
+
// 中略
|
49
|
+
|
50
|
+
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
51
|
+
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
|
52
|
+
|
53
|
+
cell.backgroundColor = self.redCellIndex == indexPath.row ? .red : .black
|
54
|
+
|
55
|
+
return cell
|
56
|
+
|
57
|
+
}
|
58
|
+
|
59
|
+
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
60
|
+
if indexPath.row == redCellIndex && OnGame{
|
61
|
+
print("赤いセル")
|
62
|
+
point = point+1
|
63
|
+
timerLabel.text="Finish"
|
64
|
+
point = point+0
|
65
|
+
// pointLabel.text="GameOver"
|
66
|
+
pointLabel.text = String(point)
|
67
|
+
timerLabel.text="0"
|
68
|
+
// pointLabel.invalidate()
|
69
|
+
// pointLabel.text="0"
|
70
|
+
|
71
|
+
} else {
|
72
|
+
print("黒いセル")
|
73
|
+
pointLabel.text = "GameOver"
|
74
|
+
timer.invalidate()
|
75
|
+
timerLabel.text = "0"
|
76
|
+
OnGame = false
|
77
|
+
// pointLabel.text="0"
|
78
|
+
}
|
79
|
+
|
80
|
+
}
|
81
|
+
|
82
|
+
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
83
|
+
return 12
|
84
|
+
}
|
85
|
+
|
86
|
+
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
|
87
|
+
let horizontalSpace : CGFloat = 0.2
|
88
|
+
let cellSize : CGFloat = self.view.bounds.width / 4 - horizontalSpace
|
89
|
+
return CGSize(width: cellSize, height: cellSize)
|
90
|
+
}
|
91
|
+
|
92
|
+
@objc func down(){
|
93
|
+
if count>=0{
|
94
|
+
count = count - 0.01
|
95
|
+
timerLabel.text = String(format: "%.2f",count)
|
96
|
+
}else{
|
97
|
+
if timer.isValid{
|
98
|
+
timer.invalidate()
|
99
|
+
timerLabel.text = "Finish"
|
100
|
+
// point = point+0
|
101
|
+
}
|
102
|
+
}
|
103
|
+
}
|
104
|
+
}
|
105
|
+
|
1
|
-
Main Story boardにCollection Viewを設置し、そこに何個かボタン(cell)を設置しました。そのcellを時間ごとに色を変えたいのですがどうすればよいのですか??
|
106
|
+
```Main Story boardにCollection Viewを設置し、そこに何個かボタン(cell)を設置しました。そのcellを時間ごとに色を変えたいのですがどうすればよいのですか??
|
2
107
|
例:6つのcellのうち5つは青1つは黄色で1秒ごとに変える
|
3
108
|
みたいな感じです。
|
4
109
|
これはタイマーとくくりつけてやるのですか?(?)
|