[iOS / Swift] Storyboard 간단한 로또앱 만들기
2024. 12. 11. 21:17
반응형
아래의 코드는 수정 전 코드임. 기능은 같음. 추가로 주석으로 처리된 코드들로도 대체 가능함.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var number1Label: UILabel!
@IBOutlet weak var number2Label: UILabel!
@IBOutlet weak var number3Label: UILabel!
@IBOutlet weak var number4Label: UILabel!
@IBOutlet weak var number5Label: UILabel!
@IBOutlet weak var number6Label: UILabel!
@IBOutlet weak var number7Label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// 이때 작성하면 크기가 정해지기 전이라 세로일 때는 모양이 꺠짐. 그래서 viewDidAppear로 이동
// number1Label.layer.cornerRadius = number1Label.bounds.width / 2
// number1Label.clipsToBounds = true // 깎인 부분 외에는 그려지지 않음
// 1 ~ 10 : 노란색
// 11 ~ 20 : 파란색
// 21 ~ 30 : 빨간색
// 31 ~ 40 : 회색
// 나머지 : 녹색
// 보너스 번호 : 보라색
let random1 = Int.random(in: 1 ... 45)
number1Label.text = "\(random1)"
switch random1 {
case 1 ... 10:
number1Label.backgroundColor = .yellow
number1Label.textColor = UIColor.black
case 11 ... 20:
number1Label.backgroundColor = .blue
number1Label.textColor = UIColor.white
case 21 ... 30:
number1Label.backgroundColor = .red
number1Label.textColor = UIColor.white
case 31 ... 40:
number1Label.backgroundColor = .gray
number1Label.textColor = UIColor.white
case 41 ... 45:
number1Label.backgroundColor = .green
number1Label.textColor = UIColor.black
default:
break
}
let random2 = Int.random(in: 1 ... 45)
number2Label.text = "\(random2)"
switch random2 {
case 1 ... 10:
number2Label.backgroundColor = .yellow
number2Label.textColor = UIColor.black
case 11 ... 20:
number2Label.backgroundColor = .blue
number2Label.textColor = UIColor.white
case 21 ... 30:
number2Label.backgroundColor = .red
number2Label.textColor = UIColor.white
case 31 ... 40:
number2Label.backgroundColor = .gray
number2Label.textColor = UIColor.white
case 41 ... 45:
number2Label.backgroundColor = .green
number2Label.textColor = UIColor.black
default:
break
}
let random3 = Int.random(in: 1 ... 45)
number3Label.text = "\(random3)"
switch random3 {
case 1 ... 10:
number3Label.backgroundColor = .yellow
number3Label.textColor = UIColor.black
case 11 ... 20:
number3Label.backgroundColor = .blue
number3Label.textColor = UIColor.white
case 21 ... 30:
number3Label.backgroundColor = .red
number3Label.textColor = UIColor.white
case 31 ... 40:
number3Label.backgroundColor = .gray
number3Label.textColor = UIColor.white
case 41 ... 45:
number3Label.backgroundColor = .green
number3Label.textColor = UIColor.black
default:
break
}
let random4 = Int.random(in: 1 ... 45)
number4Label.text = "\(random4)"
switch random4 {
case 1 ... 10:
number4Label.backgroundColor = .yellow
number4Label.textColor = UIColor.black
case 11 ... 20:
number4Label.backgroundColor = .blue
number4Label.textColor = UIColor.white
case 21 ... 30:
number4Label.backgroundColor = .red
number4Label.textColor = UIColor.white
case 31 ... 40:
number4Label.backgroundColor = .gray
number4Label.textColor = UIColor.white
case 41 ... 45:
number4Label.backgroundColor = .green
number4Label.textColor = UIColor.black
default:
break
}
let random5 = Int.random(in: 1 ... 45)
number5Label.text = "\(random5)"
switch random5 {
case 1 ... 10:
number5Label.backgroundColor = .yellow
number5Label.textColor = UIColor.black
case 11 ... 20:
number5Label.backgroundColor = .blue
number5Label.textColor = UIColor.white
case 21 ... 30:
number5Label.backgroundColor = .red
number5Label.textColor = UIColor.white
case 31 ... 40:
number5Label.backgroundColor = .gray
number5Label.textColor = UIColor.white
case 41 ... 45:
number5Label.backgroundColor = .green
number5Label.textColor = UIColor.black
default:
break
}
let random6 = Int.random(in: 1 ... 45)
number6Label.text = "\(random6)"
switch random6 {
case 1 ... 10:
number6Label.backgroundColor = .yellow
number6Label.textColor = UIColor.black
case 11 ... 20:
number6Label.backgroundColor = .blue
number6Label.textColor = UIColor.white
case 21 ... 30:
number6Label.backgroundColor = .red
number6Label.textColor = UIColor.white
case 31 ... 40:
number6Label.backgroundColor = .gray
number6Label.textColor = UIColor.white
case 41 ... 45:
number6Label.backgroundColor = .green
number6Label.textColor = UIColor.black
default:
break
}
let random7 = Int.random(in: 1 ... 45)
number7Label.text = "\(random7)"
number7Label.backgroundColor = UIColor.purple
number7Label.textColor = UIColor.white
// // 랜덤한 1~45까지의 숫자 생성
// number1Label.text = "\(Int.random(in: 1 ... 45))"
// number2Label.text = "\(Int.random(in: 1 ... 45))"
// number3Label.text = "\(Int.random(in: 1 ... 45))"
// number4Label.text = "\(Int.random(in: 1 ... 45))"
// number5Label.text = "\(Int.random(in: 1 ... 45))"
// number6Label.text = "\(Int.random(in: 1 ... 45))"
// number7Label.text = "\(Int.random(in: 1 ... 45))"
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
number1Label.layer.cornerRadius = number1Label.bounds.width / 2
number1Label.clipsToBounds = true
number2Label.layer.cornerRadius = number2Label.bounds.width / 2
number2Label.clipsToBounds = true
number3Label.layer.cornerRadius = number3Label.bounds.width / 2
number3Label.clipsToBounds = true
number4Label.layer.cornerRadius = number4Label.bounds.width / 2
number4Label.clipsToBounds = true
number5Label.layer.cornerRadius = number5Label.bounds.width / 2
number5Label.clipsToBounds = true
number6Label.layer.cornerRadius = number6Label.bounds.width / 2
number6Label.clipsToBounds = true
number7Label.layer.cornerRadius = number7Label.bounds.width / 2
number7Label.clipsToBounds = true
}
}
1차 개선한 코드
반복되는 코드들을 while문으로 정리하여 코드의 가독성을 높임
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var number1Label: UILabel!
@IBOutlet weak var number2Label: UILabel!
@IBOutlet weak var number3Label: UILabel!
@IBOutlet weak var number4Label: UILabel!
@IBOutlet weak var number5Label: UILabel!
@IBOutlet weak var number6Label: UILabel!
@IBOutlet weak var number7Label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let labels = [number1Label!, number2Label!, number3Label!, number4Label!, number5Label!, number6Label!, number7Label!] // 옵셔널을 배우지 않아서 강제언래핑 한것임.
var nums = [Int]()
while nums.count < 7 { // or labels.count
let randomNum1 = Int.random(in: 1...45)
if !nums.contains(randomNum1) {
nums.append(randomNum1)
}
}
let sortedNums = nums.sorted()
for (index, label) in labels.enumerated() {
label.layer.cornerRadius = label.bounds.width / 2
label.clipsToBounds = true
label.text = "\(sortedNums[index])"
switch sortedNums[index] {
case 1 ... 10:
label.backgroundColor = .yellow
label.textColor = UIColor.black
case 11 ... 20:
label.backgroundColor = .blue
label.textColor = UIColor.white
case 21 ... 30:
label.backgroundColor = .red
label.textColor = UIColor.white
case 31 ... 40:
label.backgroundColor = .gray
label.textColor = UIColor.white
case 41 ... 45:
label.backgroundColor = .green
label.textColor = UIColor.black
default:
break
}
}
number7Label.backgroundColor = UIColor.purple
number7Label.textColor = UIColor.white
}
}
반응형
'Study > Storyboard' 카테고리의 다른 글
[iOS / Swift] Storyboard 간단한 날씨앱 만들기 (1) | 2024.12.11 |
---|---|
[iOS / Swift] Storyboard 간단한 로그인 기능 구현 (0) | 2024.12.09 |
[iOS / Swift] Storyboard 간단한 계산기 만들기 (0) | 2024.12.04 |
Swift 계산기 만들기 (UIKit-Stroyboard) (0) | 2024.06.28 |
Swift 팀 소개 앱 만들기 (0) | 2024.06.28 |