forked from itskutush/Sign-Engage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeelingsViewController.swift
97 lines (77 loc) · 3.69 KB
/
FeelingsViewController.swift
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
//
// FeelingsViewController.swift
// SignEngagePractice
//
// Created by admin26 on 13/11/24.
//
import UIKit
class FeelingsViewController: UIViewController {
// List of feelings to display as buttons
let feelings = ["Happy", "Sad", "Anger", "Jealous", "Pleasant", "Stress", "Neutral", "Unpleasant"]
override func viewDidLoad() {
super.viewDidLoad()
// Create and configure the vertical stack view
let verticalStackView = UIStackView()
verticalStackView.axis = .vertical
verticalStackView.alignment = .fill
verticalStackView.distribution = .fillEqually
verticalStackView.spacing = 10
verticalStackView.translatesAutoresizingMaskIntoConstraints = false
// Add the vertical stack view to the main view
view.addSubview(verticalStackView)
// Set constraints for the vertical stack view to center it in the main view
NSLayoutConstraint.activate([
verticalStackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
verticalStackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
verticalStackView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
])
// Define the number of buttons per row
let buttonsPerRow = 3
var currentHorizontalStackView: UIStackView?
// Loop through the feelings and create buttons
for (index, feeling) in feelings.enumerated() {
// Every time we reach the start of a new row, create a new horizontal stack view
if index % buttonsPerRow == 0 {
currentHorizontalStackView = UIStackView()
currentHorizontalStackView?.axis = .horizontal
currentHorizontalStackView?.alignment = .fill
currentHorizontalStackView?.distribution = .fillEqually
currentHorizontalStackView?.spacing = 10
// Add the new horizontal stack to the vertical stack
if let horizontalStack = currentHorizontalStackView {
verticalStackView.addArrangedSubview(horizontalStack)
}
}
// Create the button for each feeling
let button = UIButton(type: .system)
button.setTitle(feeling, for: .normal)
button.setTitleColor(.black, for: .normal)
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.black.cgColor
button.layer.cornerRadius = 10
button.backgroundColor = .white
// Add an action to handle button tap
button.addTarget(self, action: #selector(feelingButtonTapped(_:)), for: .touchUpInside)
// Add the button to the current horizontal stack view
currentHorizontalStackView?.addArrangedSubview(button)
}
}
// Action for button tap to toggle selection color
@objc func feelingButtonTapped(_ sender: UIButton) {
if sender.backgroundColor == .systemBlue {
sender.backgroundColor = .white
sender.setTitleColor(.black, for: .normal)
} else {
sender.backgroundColor = .systemBlue
sender.setTitleColor(.white, for: .normal)
}
}
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/