Do it! 스위프트로 아이폰 앱 만들기
04장 도전! Misson 알람 시계 만들기
⇨ 선택한 시간과 현재 시간이 같아지면 1분동안 배경색이 빨간색으로 바뀌었다가 1분이 지나면 다시 흰색으로 돌아온다.
class ViewController: UIViewController {
let timeSelector: Selector = #selector(ViewController.updateTime)
let interval = 1.0
//추가 코드
var alarmTime: String?
@IBAction func changeDatePicker(_ sender: UIDatePicker) {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm EEE"
lblPickerTime.text = "선택시간: " + formatter.string(from: sender.date)
//추가 코드
formatter.dateFormat = "yyyy-MM-dd HH:mm"
alarmTime = formatter.string(from: sender.date)
}
@objc func updateTime() {
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss EEE"
lblCurrentTime.text = "현재시간: " + formatter.string(from: date)
//추가 코드
formatter.dateFormat = "yyyy-MM-dd HH:mm"
let currentTime = formatter.string(from: date)
if alarmTime == currentTime {
view.backgroundColor = UIColor.red
}
else {
view.backgroundColor = UIColor.white
}
}
import UIKit
class ViewController: UIViewController {
let timeSelector: Selector = #selector(ViewController.updateTime)
let interval = 1.0
var alarmTime: String?
@IBOutlet var lblCurrentTime: UILabel!
@IBOutlet var lblPickerTime: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
Timer.scheduledTimer(timeInterval: interval, target: self, selector: timeSelector, userInfo: nil, repeats: true)
}
@IBAction func changeDatePicker(_ sender: UIDatePicker) {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm EEE"
lblPickerTime.text = "선택시간: " + formatter.string(from: sender.date)
formatter.dateFormat = "yyyy-MM-dd HH:mm"
alarmTime = formatter.string(from: sender.date)
}
@objc func updateTime() {
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss EEE"
lblCurrentTime.text = "현재시간: " + formatter.string(from: date)
formatter.dateFormat = "yyyy-MM-dd HH:mm"
let currentTime = formatter.string(from: date)
if alarmTime == currentTime {
view.backgroundColor = UIColor.red
}
else {
view.backgroundColor = UIColor.white
}
}
}