-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.swift
More file actions
95 lines (73 loc) · 3.09 KB
/
ViewController.swift
File metadata and controls
95 lines (73 loc) · 3.09 KB
1
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// ViewController.swift
// CRC_iOS
//
// Created by 우예지 on 2021/05/02.
//
import UIKit
var transportations = ["Still", "Walking", "Manual Wheelchair", "Power Wheelchair", "Bus", "Metro", "Car"]
class ViewController: UIViewController,
UICollectionViewDataSource,
UICollectionViewDelegate,
UICollectionViewDelegateFlowLayout {
let ad = UIApplication.shared.delegate as? AppDelegate
var selectedIndex: Int?
// collection view
@IBOutlet weak var collectionView: UICollectionView!
let sectionInsects = UIEdgeInsets(top: 12, left: 12, bottom: 12, right: 12)
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
return transportations.count
}
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? CustomCell else {
return UICollectionViewCell()
}
let transportation = transportations[indexPath.item]
let transportationIcon = UIImage(named: transportation)!
cell.updateButtonIcon(icon: transportationIcon)
cell.updateLabel(text: transportation)
cell.setIndex(index: indexPath.item)
return cell
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
// let frameWidth = UIScreen.main.bounds.width
let frameWidth = collectionView.frame.width
let itemsPerRow: CGFloat = 2
let widthPadding = sectionInsects.left * (itemsPerRow + 1)
let cellWidth = (frameWidth - widthPadding) / itemsPerRow
return CGSize(width: cellWidth, height: cellWidth * 1.2)
}
// onclick event
@IBAction func onClick(_ sender: UIButton) {
// create folder
let fileCreator = FileCreator()
ad?.selectedTransportation = transportations[sender.tag]
ad?.directoryURL = fileCreator.setFolderDirectory()
// change view
let mainStoryBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let survey1View = mainStoryBoard.instantiateViewController(withIdentifier: "Survey1")
self.navigationController?.pushViewController(survey1View, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
collectionView.dataSource = self
collectionView.delegate = self
}
}
class CustomCell: UICollectionViewCell {
@IBOutlet var cellButton: UIButton!
@IBOutlet var cellLabel: UILabel!
func updateButtonIcon(icon: UIImage) {
cellButton.setImage(icon, for: .normal)
}
func updateLabel(text: String) {
cellLabel.text = text
}
func setIndex(index: Int) {
cellButton.tag = index
}
}