美文网首页
SwiftUI 学习 ScenePhase

SwiftUI 学习 ScenePhase

作者: 弑神指 | 来源:发表于2020-10-20 10:40 被阅读0次

    ScenePhase 现场阶段

    @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
    public enum ScenePhase : Comparable {
    
        /// The scene isn't currently visible in the UI.
        ///
        /// Do as little as possible in a scene that's in the `background` phase.
        /// The `background` phase can precede termination, so do any cleanup work
        /// immediately upon entering this state. For example, close any open files
        /// and network connections. However, a scene can also return to the
        /// the ``ScenePhase/active`` phase from the background.
        ///
        /// Expect an app that enters the `background` phase to terminate.
      case background
    
        /// The scene is in the foreground but should pause its work.
        ///
        /// A scene in this phase doesn't receive events and should pause
        /// timers and free any unnecessary resources. The scene might be completely
        /// hidden in the user interface or otherwise unavailable to the user.
        /// In macOS, scenes only pass through this phase temporarily on their way
        /// to the ``ScenePhase/background`` phase.
        ///
        /// An app or custom scene in this phase contains no scene instances in the
        /// ``ScenePhase/active`` phase.
        case inactive
    
        /// The scene is in the foreground and interactive.
        ///
        /// An active scene isn't necessarily front-most. For example, a macOS
        /// window might be active even if it doesn't currently have focus.
        /// Nevertheless, all scenes should operate normally in this phase.
        ///
        /// An app or custom scene in this phase contains at least one active scene
        /// instance.
        case active
    
        /// Returns a Boolean value indicating whether two values are equal.
        ///
        /// Equality is the inverse of inequality. For any values `a` and `b`,
        /// `a == b` implies that `a != b` is `false`.
        ///
        /// - Parameters:
        ///   - lhs: A value to compare.
        ///   - rhs: Another value to compare.
        public static func == (a: ScenePhase, b: ScenePhase) -> Bool
    
        /// The hash value.
        ///
        /// Hash values are not guaranteed to be equal across different executions of
        /// your program. Do not save hash values to use during a future execution.
        ///
        /// - Important: `hashValue` is deprecated as a `Hashable` requirement. To
        ///   conform to `Hashable`, implement the `hash(into:)` requirement instead.
        public var hashValue: Int { get }
    
        /// Hashes the essential components of this value by feeding them into the
        /// given hasher.
        ///
        /// Implement this method to conform to the `Hashable` protocol. The
        /// components used for hashing must be the same as the components compared
        /// in your type's `==` operator implementation. Call `hasher.combine(_:)`
        /// with each of these components.
        ///
        /// - Important: Never call `finalize()` on `hasher`. Doing so may become a
        ///   compile-time error in the future.
        ///
        /// - Parameter hasher: The hasher to use when combining the components
        ///   of this instance.
        public func hash(into hasher: inout Hasher)
    
        /// Returns a Boolean value indicating whether the value of the first
        /// argument is less than that of the second argument.
        ///
        /// This function is the only requirement of the `Comparable` protocol. The
        /// remainder of the relational operator functions are implemented by the
        /// standard library for any type that conforms to `Comparable`.
        ///
        /// - Parameters:
        ///   - lhs: A value to compare.
        ///   - rhs: Another value to compare.
        public static func < (a: ScenePhase, b: ScenePhase) -> Bool
    }
    
    @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
    extension ScenePhase : Hashable {
    }
    

    Background :

    ///场景当前在UI中不可见。
    ///
    ///在“背景”阶段的场景中尽量少做。
    ///“后台”阶段可以在终止之前,因此执行任何清理工作
    ///立即进入此状态。例如,关闭所有打开的文件
    ///和网络连接。不过,一个场景也可以回到
    ///背景中的‘ScenePhase’/‘active’阶段。
    ///
    ///期望进入“后台”阶段的应用程序终止。

    Inactive

    ///场景在前台,但应该暂停工作。
    ///
    ///这个阶段的场景不接收事件,应该暂停
    ///计时器和释放任何不必要的资源。场景可能是完全的
    ///隐藏在用户界面中或对用户不可用。
    ///在macOS中,场景只是暂时通过这个阶段
    ///转到‘ScenePhase/background’阶段。
    ///
    ///此阶段的应用程序或自定义场景不包含场景实例
    / / /‘ScenePhase /活跃的阶段。

    Active

    // 场景在前景中,相互作用。
    ///
    /// 一个活跃的场景不一定是最前面的。例如,macOS
    /// 窗口可能是活动的,即使它目前没有焦点。
    /// 不过,此阶段所有场景应正常运行。
    ///
    /// 此阶段的应用程序或自定义场景至少包含一个活动场景
    /// 实例。

    SceneStorage<Value> 场景存储

    开头如下:
    // 对每个场景进行读写的属性包装器类型
    /// 存储。
    ///
    /// 您使用' SceneStorage '时,您需要自动恢复状态
    /// 值。“SceneStorage”的工作原理与“State”非常相似,除了它的首字母
    /// 如果之前保存过,则系统恢复该值,值为·
    /// 与同一场景中的其他“SceneStorage”变量共享。
    ///
    /// 系统管理' SceneStorage '的保存和恢复
    /// 。无法使用支持“SceneStorage”的基础数据
    /// 你,所以你必须通过“SceneStorage”属性包装器访问它。的
    /// system不保证数据的时间和频率
    /// 保存。
    ///
    /// 每个“场景”都有自己的“SceneStorage”概念,所以数据不会被共享
    /// 场景之间

    ///
    /// 确保使用“SceneStorage”的数据是轻量级的。数据的
    /// 大型数据,例如模型数据,不应该存储在“SceneStorage”中
    /// 可能会导致性能下降。
    ///
    /// 如果“场景”明显被销毁(例如切换器快照被销毁)
    /// 在iPadOS上销毁或在macOS上关闭窗口),数据也是
    /// 销毁。不要对敏感数据使用“SceneStorage”。

    @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
    @frozen @propertyWrapper public struct SceneStorage<Value> : DynamicProperty {
    
        /// The underlying value referenced by the state variable.
        ///
        /// This works identically to `State.wrappedValue`.
        ///
        /// - SeeAlso: State.wrappedValue
        public var wrappedValue: Value { get nonmutating set }
    
        /// A binding to the state value.
        ///
        /// This works identically to `State.projectedValue`.
        ///
        /// - SeeAlso: State.projectedValue
        public var projectedValue: Binding<Value> { get }
    }
    
    @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
    extension SceneStorage {
    
        /// Creates a property that can save and restore a boolean.
        ///
        /// - Parameter wrappedValue: The default value if a boolean is not
        ///   available for the given key.
        /// - Parameter key: a key used to save and restore the value.
        public init(wrappedValue: Value, _ key: String) where Value == Bool
    
        /// Creates a property that can save and restore an integer.
        ///
        /// - Parameter wrappedValue: The default value if an integer is not
        ///   available for the given key.
        /// - Parameter key: a key used to save and restore the value.
        public init(wrappedValue: Value, _ key: String) where Value == Int
    
        /// Creates a property that can save and restore a double.
        ///
        /// - Parameter wrappedValue: The default value if a double is not available
        ///   for the given key.
        /// - Parameter key: a key used to save and restore the value.
        public init(wrappedValue: Value, _ key: String) where Value == Double
    
        /// Creates a property that can save and restore a string.
        ///
        /// - Parameter wrappedValue: The default value if a string is not available
        ///   for the given key.
        /// - Parameter key: a key used to save and restore the value.
        public init(wrappedValue: Value, _ key: String) where Value == String
    
        /// Creates a property that can save and restore a URL.
        ///
        /// - Parameter wrappedValue: The default value if a URL is not available
        ///   for the given key.
        /// - Parameter key: a key used to save and restore the value.
        public init(wrappedValue: Value, _ key: String) where Value == URL
    
        /// Creates a property that can save and restore data.
        ///
        /// Avoid storing large data blobs, such as image data, as it can negatively
        /// affect performance of your app.
        ///
        /// - Parameter wrappedValue: The default value if data is not available
        ///   for the given key.
        /// - Parameter key: a key used to save and restore the value.
        public init(wrappedValue: Value, _ key: String) where Value == Data
    
        /// Creates a property that can save and restore an integer, transforming it
        /// to a `RawRepresentable` data type.
        ///
        /// A common usage is with enumerations:
        ///
        ///    enum MyEnum: Int {
        ///        case a
        ///        case b
        ///        case c
        ///    }
        ///    struct MyView: View {
        ///        @SceneStorage("MyEnumValue") private var value = MyEnum.a
        ///        var body: some View { ... }
        ///    }
        ///
        /// - Parameter wrappedValue: The default value if an integer value is not
        ///   available for the given key.
        /// - Parameter key: a key used to save and restore the value.
        public init(wrappedValue: Value, _ key: String) where Value : RawRepresentable, Value.RawValue == Int
    
        /// Creates a property that can save and restore a string, transforming it
        /// to a `RawRepresentable` data type.
        ///
        /// A common usage is with enumerations:
        ///
        ///    enum MyEnum: String {
        ///        case a
        ///        case b
        ///        case c
        ///    }
        ///    struct MyView: View {
        ///        @SceneStorage("MyEnumValue") private var value = MyEnum.a
        ///        var body: some View { ... }
        ///    }
        ///
        /// - Parameter wrappedValue: The default value if a String value is not
        ///   available for the given key.
        /// - Parameter key: a key used to save and restore the value.
        public init(wrappedValue: Value, _ key: String) where Value : RawRepresentable, Value.RawValue == String
    }
    
    @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
    extension SceneStorage where Value : ExpressibleByNilLiteral {
    
        /// Creates a property that can save and restore an Optional boolean.
        ///
        /// Defaults to nil if there is no restored value
        ///
        /// - Parameter key: a key used to save and restore the value.
        public init(_ key: String) where Value == Bool?
    
        /// Creates a property that can save and restore an Optional integer.
        ///
        /// Defaults to nil if there is no restored value
        ///
        /// - Parameter key: a key used to save and restore the value.
        public init(_ key: String) where Value == Int?
    
        /// Creates a property that can save and restore an Optional double.
        ///
        /// Defaults to nil if there is no restored value
        ///
        /// - Parameter key: a key used to save and restore the value.
        public init(_ key: String) where Value == Double?
    
        /// Creates a property that can save and restore an Optional string.
        ///
        /// Defaults to nil if there is no restored value
        ///
        /// - Parameter key: a key used to save and restore the value.
        public init(_ key: String) where Value == String?
    
        /// Creates a property that can save and restore an Optional URL.
        ///
        /// Defaults to nil if there is no restored value
        ///
        /// - Parameter key: a key used to save and restore the value.
        public init(_ key: String) where Value == URL?
    
        /// Creates a property that can save and restore an Optional data.
        ///
        /// Defaults to nil if there is no restored value
        ///
        /// - Parameter key: a key used to save and restore the value.
        public init(_ key: String) where Value == Data?
    }
    

    SceneStorage<Value>

    ///对每个场景进行读写的属性包装器类型
    / / /存储。
    ///
    ///您使用' SceneStorage '时,您需要自动恢复状态
    / / /值。“SceneStorage”的工作原理与“State”非常相似,除了它的首字母
    ///如果之前保存过,则系统恢复该值,值为·
    ///与同一场景中的其他“SceneStorage”变量共享。
    ///
    ///系统管理' SceneStorage '的保存和恢复
    / / /。无法使用支持“SceneStorage”的基础数据
    ///你,所以你必须通过“SceneStorage”属性包装器访问它。的
    /// system不保证数据的时间和频率
    / / /保存。
    ///
    ///每个“场景”都有自己的“SceneStorage”概念,所以数据不会被共享
    / / /场景之间。
    ///
    ///确保使用“SceneStorage”的数据是轻量级的。数据的
    ///大型数据,例如模型数据,不应该存储在“SceneStorage”中
    ///可能会导致性能下降。
    ///
    ///如果“场景”明显被销毁(例如切换器快照被销毁)
    ///在iPadOS上销毁或在macOS上关闭窗口),数据也是
    / / /销毁。不要对敏感数据使用“SceneStorage”。

    /// Creates a property that can save and restore an integer, transforming it
    /// to a RawRepresentable data type.
    ///
    /// A common usage is with enumerations:
    ///
    /// enum MyEnum: Int {
    /// case a
    /// case b
    /// case c
    /// }
    /// struct MyView: View {
    /// @SceneStorage("MyEnumValue") private var value = MyEnum.a
    /// var body: some View { ... }
    /// get : print(value)
    /// set: value = MyEnum.b
    /// }

    EnvironmentValues 环境变量

     @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
    extension EnvironmentValues {
    
        /// The current phase of the scene.
        ///
        /// The system sets this value to provide an indication of the
        /// operational state of a scene or collection of scenes. The exact
        /// meaning depends on where you access the value. For more information,
        /// see ``ScenePhase``.
        public var scenePhase: ScenePhase
    }
    

    ///场景的当前阶段。
    ///
    ///系统设置该值以指示
    ///场景或场景集合的运行状态。确切的
    ///的含义取决于您访问值的位置。有关更多信息,
    / / /看到“ScenePhase ' '。

    1. 用在自定义场景实例中
    // If you read the phase from within a custom ``Scene`` instance, the value
    /// similarly reflects an aggregation of all the scenes that make up the custom
    /// scene:
    // 如果你从一个自定义的' ' Scene ' '实例中读取阶段,值
    /// 类似地反映了构成该习惯的所有场景的集合
    /// 场景:
    ///
    ///     struct MyScene: Scene {
    ///         @Environment(\.scenePhase) private var scenePhase
    ///
    ///         var body: some Scene {
    ///             WindowGroup {
    ///                 MyRootView()
    ///             }
    ///             .onChange(of: scenePhase) { phase in
    ///                 if phase == .background {
    ///                     // Perform cleanup when all scenes within
    ///                     // MyScene go to the background.
    ///                 }
    ///             }
    ///         }
    ///     }
    
    1. 用于App
    /// If you read the phase from within an ``App`` instance, you obtain an
    /// aggregate value that reflects the phases of all the scenes in your app. The
    /// app reports a value of ``ScenePhase/active`` if any scene is active, or a
    /// value of ``ScenePhase/inactive`` when no scenes are active. This includes
    /// multiple scene instances created from a single scene declaration; for
    /// example, from a ``WindowGroup``. When an app enters the
    /// ``ScenePhase/background`` phase, expect the app to terminate soon after.
    /// You can use that opportunity to free any resources:
    /// 如果从' ' App ' '实例中读取阶段,则获取
    /// 反映应用程序中所有场景阶段的累计值
    /// 如果任何场景是活动的,app会报告一个' ' ScenePhase/active ' '的值
    /// 当没有场景处于活动状态时' ' ScenePhase/inactive ' '的值。这包括
    /// 从一个场景声明创建多个场景实例;为
    /// 例子,来自一个' ' WindowGroup ' '。当一个应用程序进入
    ///   ' ' ScenePhase/background ' ' stage,预计应用程序将很快终止。
    /// 你可以利用这个机会来释放任何资源:
    ///
    ///     @main
    ///     struct MyApp: App {
    ///         @Environment(\.scenePhase) private var scenePhase
    ///
    ///         var body: some Scene {
    ///             WindowGroup {
    ///                 MyRootView()
    ///             }
    ///             .onChange(of: scenePhase) { phase in
    ///                 if phase == .background {
    ///                     // Perform cleanup when all scenes within
    ///                     // MyApp go to the background.
    ///                 }
    ///             }
    ///         }
    ///     }
    
    1. 用于视图,读取依赖状态
    /// How you interpret the value depends on where it's read from.
    /// If you read the phase from inside a ``View`` instance, you obtain a value
    /// that reflects the phase of the scene that contains the view. The following
    /// example uses the ``SwiftUI/View/onChange(of:perform:)`` method to enable
    /// a timer whenever the enclosing scene enters the ``ScenePhase/active`` phase
    /// and disable the timer when entering any other phase:
    /// 如何解释该值取决于从哪里读取。
    /// 如果从' ' View ' '实例内部读取阶段,则获得一个值
    /// 反映包含视图的场景的阶段。以下
    /// 示例使用' ' SwiftUI/View/onChange(of:perform:) ' '方法启用
    /// 当封闭场景进入' ' ScenePhase/active ' '阶段时提供一个计时器
    /// 和禁用计时器时,进入任何其他阶段:
    ///
    ///     struct MyView: View {
    ///         @ObservedObject var model: DataModel
    ///         @Environment(\.scenePhase) private var scenePhase
    ///
    ///         var body: some View {
    ///             TimerView()
    ///                 .onChange(of: scenePhase) { phase in
    ///                     model.isTimerRunning = (phase == .active)
    ///                 }
    ///         }
    ///     }
    
    1. 用于场景状态
    /// An indication of a scene's operational state.
    ///
    /// The system moves your app's ``Scene`` instances through phases that reflect
    /// a scene's operational state. You can trigger actions when the phase changes.
    /// Read the current phase by observing the ``EnvironmentValues/scenePhase``
    /// value in the ``Environment``:
    // 现场运行状态的指示。
    ///
    /// 系统将你的应用程序的“场景”实例通过反射的阶段移动
    ///  a场景的运行状态。当相位变化时,你可以触发动作。
    /// 通过观察' ' EnvironmentValues/scenePhase ' '来读取当前阶段
    /// 在“环境”中的价值:
    ///
    ///     @Environment(\.scenePhase) private var scenePhase
    

    相关文章

      网友评论

          本文标题:SwiftUI 学习 ScenePhase

          本文链接:https://www.haomeiwen.com/subject/jqirmktx.html