Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ViewController: UIViewController {
- // letterGuess
- // usedLetters
- // score/lives
- var scoreLabel: UILabel!
- var answerLabel: UILabel!
- var characterButtons = [UIButton]()
- let width:CGFloat = 100
- let height:CGFloat = 100
- var score = 0 {
- didSet {
- scoreLabel.text = "Score: \(score)"
- }
- }
- override func loadView() {
- view = UIView()
- view.backgroundColor = .white
- scoreLabel = UILabel()
- scoreLabel.translatesAutoresizingMaskIntoConstraints = false
- scoreLabel.textAlignment = .right
- scoreLabel.font = UIFont.systemFont(ofSize: 24)
- scoreLabel.text = "Score: 0"
- view.addSubview(scoreLabel)
- answerLabel = UILabel()
- answerLabel.translatesAutoresizingMaskIntoConstraints = false
- answerLabel.font = UIFont.systemFont(ofSize: 24)
- answerLabel.text = "ANSWER"
- answerLabel.numberOfLines = 1
- answerLabel.textAlignment = .center
- answerLabel.setContentHuggingPriority(UILayoutPriority(1), for: .vertical)
- view.addSubview(answerLabel)
- let buttonsView = UIView()
- buttonsView.translatesAutoresizingMaskIntoConstraints = false
- view.addSubview(buttonsView)
- let row1View = UIView()
- row1View.clipsToBounds = true
- row1View.translatesAutoresizingMaskIntoConstraints = false
- buttonsView.addSubview(row1View)
- let row2View = UIView()
- row2View.clipsToBounds = true
- row2View.translatesAutoresizingMaskIntoConstraints = false
- row2View.setContentHuggingPriority(.defaultLow, for: .vertical)
- buttonsView.addSubview(row2View)
- let row3View = UIView()
- row3View.clipsToBounds = true
- row3View.translatesAutoresizingMaskIntoConstraints = false
- buttonsView.addSubview(row3View)
- NSLayoutConstraint.activate([
- scoreLabel.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor),
- scoreLabel.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor, constant: 0),
- answerLabel.topAnchor.constraint(equalTo: scoreLabel.bottomAnchor, constant: 25),
- answerLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
- buttonsView.widthAnchor.constraint(equalToConstant: 1000),
- buttonsView.heightAnchor.constraint(equalToConstant: 300),
- buttonsView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
- buttonsView.topAnchor.constraint(equalTo: answerLabel.bottomAnchor, constant: 20),
- buttonsView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor, constant: -20),
- row1View.leftAnchor.constraint(equalTo: buttonsView.leftAnchor),
- row1View.topAnchor.constraint(equalTo: buttonsView.topAnchor),
- row1View.widthAnchor.constraint(equalTo: buttonsView.widthAnchor),
- row1View.heightAnchor.constraint(equalToConstant: height),
- row2View.leftAnchor.constraint(equalTo: buttonsView.leftAnchor),
- row2View.topAnchor.constraint(equalTo: row1View.bottomAnchor),
- row2View.widthAnchor.constraint(equalTo: buttonsView.widthAnchor),
- row2View.heightAnchor.constraint(equalToConstant: height),
- row3View.leftAnchor.constraint(equalTo: buttonsView.leftAnchor),
- row3View.topAnchor.constraint(equalTo: row2View.bottomAnchor),
- row3View.widthAnchor.constraint(equalTo: buttonsView.widthAnchor),
- row3View.heightAnchor.constraint(equalToConstant: height)
- ])
- var i = 10
- for row in 0..<3 {
- print(row)
- switch row {
- case 0:
- i = 10
- case 1:
- i = 9
- case 2:
- i = 7
- default:
- return
- }
- for col in 0..<i {
- let characterButton = UIButton(type: .system)
- characterButton.titleLabel?.font = UIFont.systemFont(ofSize: 36)
- characterButton.layer.borderWidth = 1
- characterButton.layer.borderColor = UIColor.lightGray.cgColor
- characterButton.layer.backgroundColor = UIColor.white.cgColor
- characterButton.setTitle("#", for: .normal)
- let frame = CGRect(x: CGFloat(col) * width, y: 0, width: width, height: height)
- characterButton.frame = frame
- switch row {
- case 0:
- print(row)
- print("row 1")
- row1View.addSubview(characterButton)
- case 1:
- print(row)
- print("row 2")
- row2View.addSubview(characterButton)
- case 2:
- print(row)
- print("row 3")
- row3View.addSubview(characterButton)
- default:
- print("defualt")
- return
- }
- characterButton.addTarget(self, action: #selector(buttonFunction), for: .touchUpInside)
- }
- }
- buttonsView.backgroundColor = .purple
- row1View.backgroundColor = .red
- row2View.backgroundColor = .yellow
- row3View.backgroundColor = .green
- }
- @objc func buttonFunction(){
- //your code goes here
- }
- }
Add Comment
Please, Sign In to add comment