【问题标题】:what's an efficient way in swift to get a "change of day" event [closed]快速获得“换日”事件的有效方法是什么[关闭]
【发布时间】:2021-07-04 04:30:11
【问题描述】:

所以我有一个 swift-ui 应用程序,我在其中显示当天的信息 - 所以在午夜我希望它切换到新的一天 - 问题是我希望它尽可能“轻松” - 例如我不希望任何东西消耗任何资源,因为我想要做的就是刷新视图(如果显示的话)。

当视图未显示时,如何在不消耗后台资源的情况下执行此操作?

【问题讨论】:

标签: swift swiftui


【解决方案1】:

你可以试试这样的: (注意我没有等到看看这是否有效)

import SwiftUI
import Combine
#if os(iOS)
import UIKit
#endif


@main
struct TestApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

struct ContentView: View {
    var body: some View {
        Text("day change")
            .onReceive(NotificationCenter.default.publisher(
                for: UIApplication.significantTimeChangeNotification)) { _ in
                
                print("----> day time has changed <----\n")
                
                print("A notification that posts when there is a significant change in time, \n for example, change to a new day (midnight), \n carrier time update, and change to or from daylight savings time.")
            }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 2021-11-10
    相关资源
    最近更新 更多