iOS

[ IOS ] ์•Œ๋žŒ ์‹œ๊ณ„ ๋งŒ๋“ค๊ธฐ - ๋ฐ์ดํ„ฐ ํ”ผ์ปค ์‚ฌ์šฉํ•ด ๋‚ ์งœ ์„ ํƒํ•˜๊ธฐ

Forest Yun 2021. 7. 14. 16:53
728x90

๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

 

๋ฌธ์ œ

๋ฐ์ดํ„ฐ ํ”ผ์ปค๋ฅผ ์ด์šฉํ•˜์—ฌ ์‹œ๊ฐ„์„ ์„ ํƒํ•˜๊ณ , ์„ ํƒ ์‹œ๊ฐ„ 1๋ถ„ ์ „๋ถ€ํ„ฐ ํ™”๋ฉด์ด ๋นจ๊ฐ„์ƒ‰๋˜๋„๋ก ๊ตฌํ˜„ํ•œ๋‹ค. ์„ ํƒํ•œ ์‹œ๊ฐ„์ด ๋˜๋ฉด ๋ฐฐ๊ฒฝ์ƒ‰์„ ์›๋ž˜ ์ƒํƒœ๋กœ ๋˜๋Œ๋ฆฐ๋‹ค.

 

 

๊ตฌํ˜„ ์ˆœ์„œ

1. ๋ ˆ์ด์•„์›ƒ ์ž‘์„ฑ

 2. ์•„์šธ๋ › ๋ณ€์ˆ˜, ์•ก์…˜ ํ•จ์ˆ˜ ์ถ”๊ฐ€

 3. ์„ ํƒ ๋‚ ์งœ์™€ ์‹œ๊ฐ„ ์ถœ๋ ฅ

 4. ํ˜„์žฌ ์‹œ๊ฐ„ ์ถœ๋ ฅ

 5. ํ˜„์žฌ ์‹œ๊ฐ„์ด ์„ ํƒ ์‹œ๊ฐ„๋˜๊ธฐ 1๋ถ„ ์ „๋ถ€ํ„ฐ ๋ฐฐ๊ฒฝํ™”๋ฉด ๋นจ๊ฐ„์ƒ‰์ด ๋˜๋„๋ก ๊ตฌํ˜„

 

 

 


1. ๋ ˆ์ด์•„์›ƒ ์ž‘์„ฑ

Lable๊ณผ DatePicker ๋ฅผ ์ด์šฉํ•ด ๋ ˆ์ด์•„์›ƒ์„ ๊ทธ๋ฆฐ๋‹ค.

 

 

 


2. ์•„์šธ๋ › ๋ณ€์ˆ˜์™€ ์•ก์…˜ ํ•จ์ˆ˜ ์ถ”๊ฐ€

 

//ViewController

import UIKit

class ViewController: UIViewController {    
    @IBOutlet var lblCurrentTime: UILabel!
    @IBOutlet var lblSelectTime: UILabel!
    
    //๋ทฐ ์ƒ์„ฑ ์‹œ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜
    override func viewDidLoad() {
        super.viewDidLoad()        
    }
    
    //DatePicker ์„ ํƒ ์‹œ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜
    @IBAction func changeDatePicker(_ sender: UIDatePicker) {
    }
}

 

 

 


3. ์„ ํƒํ•œ ๋‚ ์งœ์™€ ์‹œ๊ฐ„ ์ถœ๋ ฅํ•˜๋„๋ก ๊ตฌํ˜„

DatePicker ์„ ํƒ์‹œ ํ˜ธ์ถœ๋˜๋Š” ์•ก์…˜ํ•จ์ˆ˜ changeDatePicker์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

 

 //DatePicker ์„ ํƒ ์‹œ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜
    @IBAction func changeDatePicker(_ sender: UIDatePicker) {
        let datePickerView = sender //(1)์ธ์ˆ˜ ์ €์žฅ
        let formatter = DateFormatter() //(2)DateFomatter ํด๋ž˜์Šค ์ƒ์ˆ˜ ์„ ์–ธ
        formatter.dateFormat = "yyyy๋…„ MM์›” dd์ผ a hh์‹œ mm๋ถ„" //(3)๋‚ ์งœ ํ˜•์‹ ์ง€์ •
        
        lblSelectTime.text  = "์„ ํƒ ์‹œ๊ฐ„: " + formatter.string(from: datePickerView.date)//(4)
    }
  1. ์ „๋‹ฌ๋œ ์ธ์ˆ˜๋ฅผ ๋ณ€์ˆ˜์— ์ €์žฅํ•œ๋‹ค.
  2. ๋‚ ์งœ๋ฅผ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด ํ˜•์‹์ด ํ•„์š”ํ•œ๋ฐ, ํ˜•์‹ ์ง€์ •์„ ์œ„ํ•ด DateFomatter ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  3. DateFomatter.dateFomat ์„ ์ด์šฉํ•ด ๋‚ ์งœ ํ˜•์‹ ์ง€์ •
  4. ์•„์šธ ๋ณ€์ˆ˜.text ์— ์ธ์ˆ˜.date ๊ฐ’์„ ๋ฌธ์ž์—ด๋กœ ํ˜•๋ณ€ํ™˜ํ•˜์—ฌ ๊ฐ’ ์„ค์ •

 

 

 


4. ํ˜„์žฌ ์‹œ๊ฐ„ ์ถœ๋ ฅ๋˜๋„๋ก ๊ตฌํ˜„

ํ˜„์žฌ ์‹œ๊ฐ„์ด ๊ณ ์ •๋˜์–ด ์žˆ์ง€ ์•Š๊ณ  ๋งค์ดˆ ๊ฐฑ์‹ ๋˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํƒ€์ด๋จธ ๊ธฐ๋Šฅ์ด ํ•„์š”ํ•˜๋‹ค

Timer.scheduledTime(timeInterval: interval, target: self, 
					select: timeSelector, userInfo: nil, repeats: true)

Timer ํด๋ž˜์Šค์˜ scheduledTime ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ํƒ€์ด๋จธ๋ฅผ ์„ค์ •ํ•œ๋‹ค.

ํƒ€์ด๋จธ ์„ค์ •์„ ์œ„ํ•ด ํ•„์š”ํ•œ ์›์†Œ
timeInterval ํƒ€์ด๋จธ ๊ฐ„๊ฒฉ
target ๋™์ž‘๋  view
selector ํƒ€์ด๋จธ๊ฐ€ ๊ตฌ๋™๋  ๋•Œ ์‹คํ–‰ํ•  ํ•จ์ˆ˜
userInfo ์‚ฌ์šฉ์ž ์ •๋ณด
reapts ๋ฐ˜๋ณต์—ฌ๋ถ€

๋”ฐ๋ผ์„œ Timer.scheduledTime()๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „ interval ๊ณผ select๋ฅผ ๋งŒ๋“ค์–ด์•ผํ•œ๋‹ค.

์ฆ‰, ํƒ€์ด๋จธ ๊ฐ„๊ฒฉ๊ณผ ์‹คํ–‰ํ•  ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

 

 

ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์ „, ๋จผ์ € ํ•„์š”ํ•œ ๋ณ€์ˆ˜ ๋˜๋Š” ์ƒ์ˆ˜๋ฅผ ๋งŒ๋“ ๋‹ค.

class MissionDatePickerViewController: UIViewController {
    let timeSelector:Selector = #selector(MissionDatePickerViewContriller.updateTime)
    let interval:Double = 1.0 // 1์ดˆ ์˜๋ฏธ
    
    @IBOutlet var lblCurrentTime: UILabel!
    @IBOutlet var lblSelectTime: UILabel!
    
    .
    .
    .
}
  • ๊ณง ๋งŒ๋“ค ํ•จ์ˆ˜๋ฅผ ์ง€์ •ํ•œ selector๋ฅผ ํ•˜๋‚˜ ์ƒ์„ฑํ•œ๋‹ค.
  • ํƒ€์ด๋จธ์— ์‚ฌ์šฉ๋  interval๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

 

 

ํƒ€์ด๋จธ ๊ตฌ๋™ ์‹œ ํ˜ธ์ถœ๋  ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด๋ณด์ž

    @objc func updateTime(){
        let currentDate:NSDate = NSDate() //ํ˜„์žฌ ์‹œ๊ฐ„์„ ๊ฐ€์ ธ์˜ค๋Š” ํด๋ž˜์Šค
        let fomatter: DateFormatter = DateFormatter() // ๋‚ ์งœ ํ˜•์‹์„ ์ง€์ •ํ•ด ์ค„ ํด๋ž˜์Šค
        fomatter.dateFormat = "yyyy๋…„ MM์›” dd์ผ a hh์‹œ mm๋ถ„ s์ดˆ" //๋‚ ์งœ ํ˜•์‹ ์ง€์ •
        
        lblCurrentTime.text = "ํ˜„์žฌ ์‹œ๊ฐ„: " + fomatter.string(from: currentDate as Date)
    }
    //#selector()์˜ ์ธ์ž๋กœ ์‚ฌ์šฉ๋  ๋ฉ”์†Œ๋“œ๋ฅผ ์„ ์–ธํ•  ๋•Œ Object-C์™€์˜ ํ˜ธํ™˜์„ฑ์„ ์œ„ํ•˜์—ฌ ํ•จ์ˆ˜ ์•ž์— @objc ํ‚ค์›Œ๋“œ ํ•„์ˆ˜
  • ์‚ฌ์šฉ ๋ชฉ์  : ํƒ€์ด๋จธ๊ฐ€ ๊ตฌ๋™๋  ๋•Œ ์‹คํ–‰
  • ํ•„์š”ํ•œ ๊ธฐ๋Šฅ : ํ˜„์žฌ ์‹œ๊ฐ„ ๊ฐฑ์‹ 
  • ๊ทธ๋ ‡๋‹ค๋ฉด ๋ฌด์—‡์„ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š”๊ฐ€? lblCurrentTime.text์— ํ˜„์žฌ ์‹œ๊ฐ„์„ ์ €์žฅํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.
  • ํ•„์š”ํ•œ ๊ฒƒ์€? ํ˜„์žฌ ์‹œ๊ฐ„์„ ๊ฐ€์ ธ์™€์ค„ ํด๋ž˜์Šค, NSDate ํด๋ž˜์Šค + ์ถ”๊ฐ€์ ์œผ๋กœ ๋‚ ์งœ ํ˜•์‹์„ ์ง€์ •ํ•  DateFomatter ํด๋ž˜์Šค  

+ #selector()์˜ ์ธ์ž๋กœ ์‚ฌ์šฉ๋  ๋ฉ”์†Œ๋“œ๋ฅผ ์„ ์–ธํ•  ๋•Œ Object-C์™€์˜ ํ˜ธํ™˜์„ฑ์„ ์œ„ํ•˜์—ฌ ํ•จ์ˆ˜ ์•ž์— @objc ํ‚ค์›Œ๋“œ ํ•„์ˆ˜

 

 

 

 


5. ํ˜„์žฌ ์‹œ๊ฐ„์ด ์„ ํƒ ์‹œ๊ฐ„๋˜๊ธฐ 1๋ถ„ ์ „๋ถ€ํ„ฐ ๋ฐฐ๊ฒฝํ™”๋ฉด ๋นจ๊ฐ„์ƒ‰์ด ๋˜๋„๋ก ๊ตฌํ˜„

ํ˜„์žฌ ์‹œ๊ฐ„ == ์„ ํƒ ์‹œ๊ฐ„ -1  ์ผ๋•Œ ๋ฐฐ๊ฒฝ ํ™”๋ฉด์„ ๋นจ๊ฐ„์ƒ‰์œผ๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.

 

๋จผ์ € ํ˜„์žฌ ์‹œ๊ฐ„๊ณผ ์•Œ๋žŒ๋  ์‹œ๊ฐ„์„ ์ €์žฅํ•  ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•œ๋‹ค.

class MissionDatePickerViewController: UIViewController {
    ...
    var currentTime: String = String() //ํ˜„์žฌ ์‹œ๊ฐ„์„ ์ €์žฅํ•  ๋ณ€์ˆ˜
    var alarmTime: String = String()   //์•Œ๋žŒ๋  ์‹œ๊ฐ„์„ ์ €์žฅํ•  ๋ณ€์ˆ˜
    
    @IBOutlet var lblCurrentTime: UILabel!
    @IBOutlet var lblSelectTime: UILabel!
    
    .
    .
    .
}

 

 

๊ทธ๋ฆฌ๊ณ  changeDatePicker์™€ updateTime์—์„œ ๊ฐ๊ฐ alarmTime๊ณผ currenTime์— ๊ฐ’์„ ์ €์žฅํ•œ๋‹ค.

updateTime ๋ฉ”์†Œ๋“œ์—์„œ๋Š” currentTime ๋ณ€์ˆ˜๋ฅผ ์œ„ํ•œ formatter๋ฅผ ๋”ฐ๋กœ ์ƒ์„ฑํ•ด ์„ค์ •ํ•ด์ค€๋‹ค.

//DatePicker ์„ ํƒ ์‹œ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜
    @IBAction func changeDatePicker(_ sender: UIDatePicker) {
        let datePickerView = sender //์ธ์ˆ˜ ์ €์žฅ
        let formatterDate = DateFormatter() //DateFomatter ํด๋ž˜์Šค ์ƒ์ˆ˜ ์„ ์–ธ
        formatterDate.dateFormat = "yyyy๋…„ MM์›” dd์ผ a hh์‹œ mm๋ถ„" //๋‚ ์งœ ํ˜•์‹ ์ง€์ •
        
        lblSelectTime.text  = formatterDate.string(from: datePickerView.date)
        alarmTime = formatterDate.string(from: datePickerView.date - 60) //์•Œ๋žŒ๋  ์‹œ๊ฐ„์„ ์ €์žฅํ•œ๋‹ค.
    }
    
    //ํƒ€์ด๋จธ๊ฐ€ ๊ตฌ๋™๋ ๋•Œ ์‹คํ–‰ํ•  ํ•จ์ˆ˜
    @objc func updateTime(){
        let currentDate:NSDate = NSDate() 
        let fomatterDate: DateFormatter = DateFormatter()
        let formatterTime = DateFormatter()
        fomatterDate.dateFormat = "yyyy๋…„ MM์›” dd์ผ a hh์‹œ mm๋ถ„ s์ดˆ" 
        formatterTime.dateFormat = "yyyy๋…„ MM์›” dd์ผ a hh์‹œ mm๋ถ„"
        
        lblCurrentTime.text = fomatterDate.string(from: currentDate as Date)
        currentTime = formatterTime.string(from: currentDate as Date)
        
    }

 

 

 

ํ˜„์žฌ ์‹œ๊ฐ„๊ณผ ์•Œ๋žŒ์‹œ๊ฐ„์„ ๋น„๊ตํ•ด ์ค„ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.

import UIKit

class MissionDatePickerViewController: UIViewController {
    
    .
    .
    .
    
    //ํ˜„์žฌ ์‹œ๊ฐ„๊ณผ ์•Œ๋žŒ ์‹œ๊ฐ„์„ ๋น„๊ตํ•˜๋Š” ํ•จ์ˆ˜
    func compareTime(){
        var backgroundColor: UIColor
        
        if(currentTime == alarmTime){
            backgroundColor = UIColor.red
        }
        else{
            backgroundColor = UIColor.white
        }
        view.backgroundColor = backgroundColor
    }
}

 

 

 

compareTime ํ•จ์ˆ˜๋Š” ๋งค interval ๊ฐฑ์‹  ๋•Œ๋งˆ๋‹ค ํ˜„์žฌ ์‹œ๊ฐ„๊ณผ ์•Œ๋žŒ ์‹œ๊ฐ„์„ ๋น„๊ตํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํƒ€์ด๋จธ๊ฐ€ ๊ตฌ๋™๋  ๋•Œ ์‹คํ–‰ํ•˜๋Š” updateTime ํ•จ์ˆ˜์—์„œ ํ˜ธ์ถœ๋˜๋„๋ก ํ•œ๋‹ค.

@objc func updateTime(){
        let currentDate:NSDate = NSDate() //ํ˜„์žฌ ์‹œ๊ฐ„์„ ๊ฐ€์ ธ์˜ค๋Š” ํด๋ž˜์Šค
        let fomatterDate: DateFormatter = DateFormatter() // ๋‚ ์งœ ํ˜•์‹์„ ์ง€์ •ํ•ด ์ค„ ํด๋ž˜์Šค
        let formatterTime = DateFormatter()
        fomatterDate.dateFormat = "yyyy๋…„ MM์›” dd์ผ a hh์‹œ mm๋ถ„ s์ดˆ" //๋‚ ์งœ ํ˜•์‹ ์ง€์ •
        formatterTime.dateFormat = "yyyy๋…„ MM์›” dd์ผ a hh์‹œ mm๋ถ„"
        
        lblCurrentTime.text = fomatterDate.string(from: currentDate as Date)
        currentTime = formatterTime.string(from: currentDate as Date)
        
        compareTime()
    }

 

 

 

 

 

 

 

 

๊ณต๋ถ€ํ•œ ์ž๋ฃŒ

Do It! ์Šค์œ„ํ”„ํŠธ๋กœ ์•„์ดํฐ ์•ฑ ๋งŒ๋“ค๊ธฐ ๊ฐœ์ • 5ํŒ
728x90