UIView - 게임 화면으로 터치와 연결되는 view
FingerGameView
게임 View 세팅
// 터치와 뷰를 연결해주는 dictionary
var touchToRoundView: [UITouch: UIView] = [:]
weak var controller: FingerGameViewController?
터치 시작
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 80, height: 80))
view.backgroundColor = UIColor(named: "orange")
view.layer.cornerRadius = 40 // 사이즈의 절반 입력 시 원 형태
view.center = touch.location(in: self) // 터치 위치를 받아 뷰의 좌표로 연결
self.touchToRoundView[touch] = view // 원 형태를 뷰에 연결
self.addSubview(view) // 뷰에 원 형태 뷰 삽입
self.controller?.touchCountDidChange()
}
}
터치 이동
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let view = touchToRoundView[touch] // 터치에 해당하는 원 형태 뷰 불러오기
view?.center = touch.location(in: self) // 원 형태 뷰 위치 업데이트
}
}
터치 끝
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let view = touchToRoundView[touch] // 터치에 해당하는 원 형태 뷰 불러오기
view?.removeFromSuperview() // 터치가 끝나면 원 형태 뷰 없애기
touchToRoundView.removeValue(forKey: touch) // 터치 - 원 형태 뷰 dictionary 에서 해당 뷰 삭제
self.controller?.touchCountDidChange()
}
}
터치가 비정상적인 이유로 끝남
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let view = touchToRoundView[touch] // 터치에 해당하는 원 형태 뷰 불러오기
view?.removeFromSuperview()
touchToRoundView.removeValue(forKey: touch)
self.controller?.touchCountDidChange()
}
}
FingerGameViewController
GameView 연결
@IBOutlet weak var gameView: FingerGameView!
...
override func viewDidLoad() {
super.viewDidLoad()
gameView.controller = self
}
터치되면 5 초간 타이머 작동 및 터치 취소 시 타이머 리셋
5 초가 지나면 터치된 곳의 원형 뷰 변형
func touchCountDidChange() {
timer?.invalidate()
timer = Timer.scheduledTimer(withTimeInterval: 5, repeats: false, block: { (t) in
let roundViews = [UIView](self.gameView.touchToRoundView.values)
// 터치 개수가 0이면 반응 무
if roundViews.count == 0{
return
}
let randomChoice = Int.random(in: 0..<roundViews.count)
for i in 0..<roundViews.count {
let roundView = roundViews[i]
UIView.animate(withDuration: 1) {
if i == randomChoice {
roundView.backgroundColor = UIColor(named: "pink")
let center = roundView.center
roundView.frame.size = CGSize(width: 120, height: 120)
roundView.center = center
roundView.layer.cornerRadius = 60
} else {
roundView.backgroundColor = UIColor(named: "green")
}
}
}
})
resetSecondsTimer()
}
타이머 리셋
func resetSecondsTimer() {
secondsTimer?.invalidate()
if gameView.touchToRoundView.count > 0 {
secondsLeft = 5
secondsLabel.text = "5"
self.blinkTimerLabel()
secondsTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { (t) in
self.secondsLeft -= 1
self.secondsLabel.text = "\(self.secondsLeft)"
self.blinkTimerLabel()
if self.secondsLeft == 0 {
self.secondsLabel.text = ""
self.secondsTimer?.invalidate()
self.blinkTimerLabel()
}
})
} else {
self.secondsLabel.text = ""
self.blinkTimerLabel()
}
}
글자 깜빡이는 효과
func blinkTimerLabel() {
self.secondsLabel.alpha = 1
UIView.animate(withDuration: 0.5) {
self.secondsLabel.alpha = 0
}
}
오늘로 마지막 과제까지 완료하였다.
강의의 80 % 이상을 완료하여 한이음에서 수강권을 또 주었다.
한이음에서는 총 세개의 수강권을 준다.
iOS 강의는 기초 강의뿐이라 나머지 2개의 수강권은 아마 2개 단계 커리큘럼의 리액트 네이티브 수업으로 듣게될 것 같다.
하지만 django 수업에도 관심이 있어 고민이 된다.
iOS 중급 - 심화 단계가 없어 조금 아쉽지만 기초 수업을 들으면서 기초를 다지고 어플리케이션 개발의 무궁무진함을 또 깨달았다...😓
알면 알수록 부족함을 느끼게되고 더욱 꾸준히 공부해야겠다는 다짐을 되새기게 되는 것 같다.
참고로 한이음 프로젝트를 안드로이드로 진행하면서 느끼는 점은 안드로이드보다 iOS 개발이 조금 더 어려운 것 같다..ㅎㅎ