์๋ ํ์ธ์ Foma ์ ๋๋ค!
์ค๋์ ํ์ด๋จธ ๊ธฐ๋ฅ์ ๊ตฌํํ๋ค๊ฐ ํ๋ฉด์ ์ ๊ทธ๊ฑฐ๋ ๋ฐฑ๊ทธ๋ผ์ด๋๋ก ๊ฐ๋๋ ํ์ด๋จธ ์๊ฐ์ด ์ ์๋๋์ง ์๋๊ฑฐ์์.
๊ทธ๋์ ์ด๊ฒ ๋ฐฑ๊ทธ๋ผ์ด๋์์๋ ์๋์๊ฐ๋๊ฑด๊ฐ? ์ด๋ป๊ฒ ํด์ผํ์ง? ํ๋ฉด์ ๊ตฌ๊ธ๋ง์ ํ๋ค๋ณด๋ "์ฑ์ด ํฌ์ด๊ทธ๋ผ์ด๋์ ์์ง ์์ผ
๋ฉด 100% ์๋๋์ง ์์ ์ ์๋ค" ๋ผ๊ณ ํ๋๋ผ๊ตฌ์. (์ด๋ป๊ฒ ๋ณด๋ฉด ๋น์ฐํ ์๋ฆฌ์ผ์๋...)
๊ทธ๋์ ์ด๋ป๊ฒ ๋ฐฑ๊ทธ๋ผ์ด๋์ ๋ค์ด๊ฐ์ ๋๋ ์ ์์ ์ผ๋ก ์๋ํ๋ ๊ฒ์ฒ๋ผ ๋ณด์ด๊ฒ ํ ๊น? ์ ๋ํด์ ์๊ฐํ๋ค๊ฐ ์ ๋๋ฆ์
ํด๊ฒฐ๋ฐฉ๋ฒ์ ์์๋ด์ ๊ณต์ ๋๋ฆฌ๊ณ ์ถ์ด์ ๊ธ์ ์์ฑํ๊ฒ ๋์์ต๋๋ค!
์ ๊ทธ๋ฌ๋ฉด ๋ฐ๋ก ์ด๋ป๊ฒ ํ๋์ง ์ค๋ช ๋๋ฆฌ๊ฒ ์ต๋๋ค.
๋จผ์ ์์ ์ ์ฑ๋ผ์ดํ์ฌ์ดํด์ ๋ํด์ ๊ณต๋ถํ์๋๋ฐ์. (ํน์ ๋ชจ๋ฅด์๋ ๋ถ๋ค์ ์ฌ๊ธฐ ์์ ํ์ธํด์ฃผ์ธ์~)
SceneDelgate๋ฅผ ํตํด์ ์ฑ์ด ๋ฐฑ๊ทธ๋ผ์ด๋์ ๋ค์ด๊ฐ์ ๋๋ ํฌ์ด๊ทธ๋ผ์ด๋์ ๋ค์ด๊ฐ์ ๋๋ฅผ ์ ์ ์์์ต๋๋ค.
์ด๊ฒ์ ์ด์ฉํด์ ๋ฐฑ๊ทธ๋ผ์ด๋์ ๊ฐ ์๊ฐ์ ์ ์ฅํด๋๊ณ ํฌ์ด๊ทธ๋ผ์ด๋์ ๋ค์ด์์ ๋ ์ฐจ์ด๋ฅผ ๊ณ์ฐํด์ ๋ํด์ฃผ๋ฉด ๋ฉ๋๋ค.
"ํฌ์ด๊ทธ๋ผ์ด๋์ ๋ค์ด์จ ์๊ฐ์์ ๋ฐฑ๊ทธ๋ผ์ด๋์ ๊ฐ ์๊ฐ์ ์ฐจ์ด๋ฅผ ์ด๋ป๊ฒ ๊ตฌํ์ง?"
์ฐ์ ๋ฐฑ๊ทธ๋ผ์ด๋์ ๋ค์ด๊ฐ์ ๋ Date()๋ฅผ ์ด์ฉํด์ ํ์ฌ ๋ ์ง์ ์๊ฐ์ ๊ธฐ์ตํด์ค๋๋ค.
๊ทธ๋ฆฌ๊ณ ํฌ์ด๊ทธ๋ผ์ด๋์ ๋ค์ ๋์์์ ๋ timeIntervalSince๋ฅผ ์ด์ฉํด์ ์ฐจ์ด๋ฅผ ๊ตฌํด์ค๋๋ค.
๋๋ ์๊ฐ.timeIntervalSince(์์ํ ์๊ฐ) <- ์ด์ ๊ฐ์ด ๋๋ ์๊ฐ์ ์์ ์ฐ๊ณ .timeIntervalSince ๊ดํธ ์์ ์์ํ ์๊ฐ์ ์ฐ๋ฉด
์๊ฐ์ ์ฐจ์ด๊ฐ ๋๋ธ๊ฐ์ผ๋ก ๋์ค๊ฒ ๋ฉ๋๋ค.
SceneDelegate
์ ๋ ์ด๋ง ๊ตฌํ ์์ ์ด์์ผ๋ฏ๋ก Intํ์ผ๋ก ๋ณํํด์ฃผ์ด์ interval์ด๋ผ๋ ๋ณ์ ์์ ์ฐจ์ด๋ฅผ ์ ์ฅํด๋์์ต๋๋ค.
"๋ฐฑ๊ทธ๋ผ์ด๋์ ๋ค์ด๊ฐ ์๊ฐ์ ์ด๋ป๊ฒ ์ ์ฅํ์ง?"
์ ๊ฐ ์ฌ์ฉํ ๋ฐฉ๋ฒ์ UserDefault๋ฅผ ์ด์ฉํ๋ ๊ฒ์ด์์ต๋๋ค.
UserDefault๋ ๋๋ฐ์ด์ค์ ์์๋ก ๊ฐ์ ์ ์ฅํ๊ณ ํค๊ฐ์ผ๋ก ์ด ๊ฐ์ ๊บผ๋ด์ ์ธ ์ ์๋ ์ ์ฅ์์ ๋๋ค!
์ด๊ณณ์ ๊ฐ์ ์ฐ๋ ๋ฐฉ๋ฒ์ ์๋์ ๊ฐ์ด ์ํ๋ ๊ฐ์ ์ ๋ ฅํ๊ณ ์ด๊ฒ์ ๊บผ๋ด์ธ ํค๊ฐ์ string์ผ๋ก ์ ํด์ฃผ์๋ฉด ๋ฉ๋๋ค.
"ํค๊ฐ์ผ๋ก ์ด๋ป๊ฒ ๊บผ๋ด์ ์ฐ์ง?"
์ ์ฅํ ๊ฐ์ด string์ผ ๊ฒฝ์ฐ์ UserDefault.standard.string() bool์ผ ๊ฒฝ์ฐ์ UserDefault.standard.bool()
์ด์ ๊ฐ์ด ์ ์ฅํ ๊ฐ์ ๋ฐ๋ผ์ ๋ฌ๋ผ์ง๋๋ฐ์. ๊ทธ๋ผ ์ ๊ฐ ์ ์ฅํ ๋ฐ์ดํธ๋ ์ด๋ป๊ฒ ๋ถ๋ฌ์ฌ๊น์?
๋ฐ๋ก ์ค๋ธ์ ํธ๋ก ๋ถ๋ฌ์์ Date๋ก ํ๋ณํ์ ์์ผ์ค๋๋ค.
๋ฐฑ๊ทธ๋ผ์ด๋์ ์๊ฐ์ ์ ์ฅํ๊ณ ๊บผ๋ด์ฐ๋ ๋ฐฉ๋ฒ๋ ์์๊ณ ํฌ์ด๊ทธ๋ผ์ด๋์ ๋ค์ด๊ฐ ์๊ฐ์์ ๋ฐฑ๊ทธ๋ผ์ด๋์ ๊ฐ ์๊ฐ์ ์ฐจ์ด๋ ๊ตฌํ์ต๋๋ค.
๊ทธ๋ผ ๊ฐ์ฅ ๊ถ๊ธํดํ์ค ๊ฑด ์๋ ์ง๋ฌธ์ด๋ผ๊ณ ์๊ฐํฉ๋๋ค.
"๊ทธ๋ผ ์ด ์๊ฐ์ ์ด๋ป๊ฒ ๋ทฐ์ปจํธ๋กค๋ฌ์ ์๋ ค์ฃผ๋๊ฑด๋ฐ?"
๋ฐ๋ก NotificationCenter๋ฅผ ์ด์ฉํ๋ ๊ฒ์ ๋๋ค.
NotificationCenter๋ ์ด๋ค ์ผ์ด ์ผ์ด๋ฌ์ ๋ ์๋ฆผ์ ์ค ์ ์๊ณ ๋ค๋ฅธ ๊ณณ์์ addObserver๋ฅผ ์ฌ์ฉํ์ฌ ์๋ฆผ์ ๋ฐ์ ์ ์๋ ํด๋์ค์ ๋๋ค.
(์ฌ์ฉ๋ฐฉ๋ฒ์ ์ฌ๊ธฐ ๋ฅผ ์ฐธ์กฐํด์ฃผ์ธ์.)
์๋์ ๊ฐ์ด ๋ฐฑ๊ทธ๋ผ์ด๋์ ๋ค์ด์์ ๋์ ํฌ์ด๊ทธ๋ผ์ด๋๋ก ๋์๊ฐ ๋ ๊ฐ๊ฐ Notification์ postํด์ค๋๋ค.
์ ์ฌ๊ธฐ์ ์๊น ๊ตฌํ ์๊ฐ์ ์ฐจ์ด๋ NotificationCenter์ userInfo๋ฅผ ํตํด์ dictionaryํํ๋ก ๋ณด๋ผ ์ ์์ต๋๋ค.
(ํน์ NotificationCenter์์ ๊ฐ์ ๋ณด๋ด๋ ๋ฐฉ๋ฒ์ ๋ชจ๋ฅด์๋ ๋ถ์ ์ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ํด์ฃผ์ธ์!)
1
2
3
4
5
6
7
8
9
10
11
|
func sceneWillEnterForeground(_ scene: UIScene) {
guard let start = UserDefaults.standard.object(forKey: "sceneDidEnterBackground") as? Date else { return }
let interval = Int(Date().timeIntervalSince(start))
NotificationCenter.default.post(name: NSNotification.Name("sceneWillEnterForeground"), object: nil,userInfo: ["time" : interval])
}
func sceneDidEnterBackground(_ scene: UIScene) {
NotificationCenter.default.post(name: NSNotification.Name("sceneDidEnterBackground"), object: nil)
UserDefaults.standard.setValue(Date(), forKey: "sceneDidEnterBackground")
}
|
ViewController
์ด์ ๋ทฐ์ปจํธ๋กค๋ฌ๋ก ์ด๋ํด์ ์๊น ๋ณด๋ธ ์๋ฆผ์ addObserver๋ฅผ ๋ฌ์์ค ๊ด์ฐฐํด์ค๋๋ค. (setnotification์ viewDidLoad์ ์คํ์์ผ์ค์ผํฉ๋๋ค.)
1
2
3
4
5
6
|
func setNotifications() {
//๋ฐฑ๊ทธ๋ผ์ด๋์์ ํฌ์ด๊ทธ๋ผ์ด๋๋ก ๋์์ฌ๋
NotificationCenter.default.addObserver(self, selector: #selector(addbackGroundTime(_:)), name: NSNotification.Name("sceneWillEnterForeground"), object: nil)
//ํฌ์ด๊ทธ๋ผ์ด๋์์ ๋ฐฑ๊ทธ๋ผ์ด๋๋ก ๊ฐ๋
NotificationCenter.default.addObserver(self, selector: #selector(stopTimer), name: NSNotification.Name("sceneDidEnterBackground"), object: nil)
}
|
๊ฐ๊ฐ @objc ํจ์๋ ์๋์ ๊ฐ์ด ์ค์ ํด์ค๋๋ค. (ํจ์ ์์ ์๋ ๋ด์ฉ์ ๊ฐ์ ์ํฉ์ ๋ง๊ฒ ๋ฐ๊พธ์ ์ผํฉ๋๋ค!)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@objc func addbackGroundTime(_ notification:Notification) {
if startorstop == true {
//๋
ธํฐํผ์ผ์ด์
์ผํฐ๋ฅผ ํตํด ๊ฐ์ ๋ฐ์์ด
let time = notification.userInfo?["time"] as? Int ?? 0
//๋ฐ์์จ ์๊ฐ์ 60์ผ๋ก ๋๋ ๋ชซ์ ๋ถ
minutes += time/60
//๋ฐ์์จ ์๊ฐ์ 60์ผ๋ก ๋๋ ๋๋จธ์ง๋ ์ด
seconds += time%60
setLottieAnimation(play: true)
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(TimerViewController.keepTimer), userInfo: nil, repeats: true)
}
}
@objc func stopTimer() {
timer.invalidate()
}
|
์๋ ์์์ ๋ณด์๋ฉด ๋ฐฑ๊ทธ๋ผ์ด๋์ ๋ค์ด๊ฐ๋ค ๋ค์ ๋์์์ ๋ ํ์ด๋จธ๊ฐ ๊ทธ ์๊ฐ๋งํผ ๋ํด์ง๊ณ ์คํ๋๋ ๊ฒ์ ๋ณผ ์ ์์ต๋๋ค!!
'๐ iOS > iOS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[iOS] ํด๋์ค์ด๋ฆ ํ๋ฒ์ ๋ชจ๋ ๋ฐ๊พธ๊ธฐ (Refactor Class name) (0) | 2020.12.14 |
---|---|
[iOS ๋ฉด์ ์ง๋ฌธ] Cocoa Touch Framework๋? (0) | 2020.12.08 |
[iOS ๋ฉด์ ์ง๋ฌธ] Retain Cycle์ด๋?(2) - Strong,Weak,Unowned (0) | 2020.12.04 |
[iOS] ํ๋ฉด ํ์คํฌ๋ฆฐ์ผ๋ก ๋ง๋๋๋ฒ (0) | 2020.12.01 |
[iOS]Cell ํญํ์ ๋ Segue๋ก ๋ฐ์ดํฐ ๋ณด๋ด๋ ๋ฒ (didSelect & Pass Data by Segue) (0) | 2020.11.30 |
๋๊ธ