美文网首页
SwiftUI 学习 Scene

SwiftUI 学习 Scene

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

    App

    @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
    public protocol App {
    
        /// The type of scene representing the content of the app.
        ///
        /// When you create a custom app, Swift infers this type from your
        /// implementation of the required ``SwiftUI/App/body-swift.property``
        /// property.
        associatedtype Body : Scene
    
        /// The content and behavior of the app.
        ///
        /// For any app that you create, provide a computed `body` property that
        /// defines your app's scenes, which are instances that conform to the
        /// ``SwiftUI/Scene`` protocol. For example, you can create a simple app
        /// with a single scene containing a single view:
       // 对于你创建的任何应用程序,提供一个计算过的' body '属性
      ///  定义应用程序的场景,这些场景是符合的实例
      /// 'SwiftUI/Scene'协议。例如,您可以创建一个简单的应用程序
      /// 单个场景包含单个视图:
        ///
        ///     @main
        ///     struct MyApp: App {
        ///         var body: some Scene {
        ///             WindowGroup {
        ///                 Text("Hello, world!")
        ///             }
        ///         }
        ///     }
        ///
        /// Swift infers the app's ``SwiftUI/App/Body-swift.associatedtype``
        /// associated type based on the scene provided by the `body` property.
        @SceneBuilder var body: Self.Body { get }
    
        /// Creates an instance of the app using the body that you define for its
        /// content.
        ///
        /// Swift synthesizes a default initializer for structures that don't
        /// provide one. You typically rely on the default initializer for
        /// your app.
        init()
    }
    
    @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
    extension App {
    
        /// Initializes and runs the app.
        ///
        /// If you precede your ``SwiftUI/App`` conformer's declaration with the
        /// [@main](https://docs.swift.org/swift-book/ReferenceManual/Attributes.html#ID626)
        /// attribute, the system calls the conformer's `main()` method to launch
        /// the app. SwiftUI provides a
        /// default implementation of the method that manages the launch process in
        /// a platform-appropriate way.
    // 初始化并运行应用程序。
    ///
    /// 如果您在“SwiftUI/App”conformer的声明前加上
    /// [@main] (https://docs.swift.org/swift-book/ReferenceManual/Attributes.html # ID626)
    /// 属性时,系统调用conformer的' main() '方法启动
    /// SwiftUI提供了一个
    /// 中管理启动进程的方法的默认实现
    /// 适合平台的方式。
        public static func main()
    }
    

    WindwoGroup

    /// A scene that presents a group of identically structured windows.
    ///
    /// Use a `WindowGroup` as a container for a view hierarchy presented by your
    /// app. The hierarchy that you declare as the group's content serves as a
    /// template for each window that the app creates from that group:
    // 呈现一组结构相同的窗口的场景。
    ///
    /// 使用' WindowGroup '作为视图层次结构的容器
    /// 您声明为组内容的层次结构充当
    /// 应用程序从该组创建的每个窗口模板:
    ///
    ///     @main
    ///     struct Mail: App {
    ///         var body: some Scene {
    ///             WindowGroup {
    ///                 MailViewer() // Declare a view hierarchy here.
    ///             }
    ///         }
    ///     }
    @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
    public struct WindowGroup<Content> : Scene where Content : View {}
    

    Scene

    @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
    public protocol Scene {
    
        /// The type of scene that represents the body of this scene.
        ///
        /// When you create a custom scene, Swift infers this type from your
        /// implementation of the required ``SwiftUI/Scene/body-swift.property``
        /// property.
        associatedtype Body : Scene
    
        /// The content and behavior of the scene.
        ///
        /// For any scene that you create, provide a computed `body` property that
        /// defines the scene as a composition of other scenes. You can assemble a
        /// scene from primitive scenes that SwiftUI provides, as well as other
        /// scenes that you've defined.
        ///
        /// Swift infers the scene's ``SwiftUI/Scene/Body-swift.associatedtype``
        /// associated type based on the contents of the `body` property.
       /// 场景的内容和行为。
      ///
     /// 对于你创建的任何场景,提供一个计算过的' body '属性
     /// 将场景定义为其他场景的组成。你可以组装一个
     /// 来自SwiftUI提供的原始场景的场景,以及其他
     /// 您定义的场景。
     ///
     /// Swift推断场景的' ' SwiftUI/ scene / body - speed .associatedtype ' '
     /// 基于“body”属性内容的关联类型。
        @SceneBuilder var body: Self.Body { get }
    }
    
    @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
    extension Scene {
    
        /// Adds an action to perform when the given value changes.
        ///
        /// Use this modifier to trigger a side effect when a value changes, like
        /// the value associated with an ``SwiftUI/Environment`` key or a
        /// ``SwiftUI/Binding``. For example, you can clear a cache when you notice
        /// that a scene moves to the background:
       /// 添加给定值更改时执行的操作。
       ///
      /// 当值更改时,使用此修饰符触发副作用,例如
      /// 与' ' SwiftUI/Environment ' '键关联的值
      /// ‘SwiftUI /绑定”。例如,您可以在注意到时清除缓存
       ///场景移到背景:
        ///
        ///     struct MyScene: Scene {
        ///         @Environment(\.scenePhase) private var scenePhase
        ///         @StateObject private var cache = DataCache()
        ///
        ///         var body: some Scene {
        ///             WindowGroup {
        ///                 MyRootView()
        ///             }
        ///             .onChange(of: scenePhase) { newScenePhase in
        ///                 if newScenePhase == .background {
        ///                     cache.empty()
        ///                 }
        ///             }
        ///         }
        ///     }
        ///
        /// The system calls the `action` closure on the main thread, so avoid
        /// long-running tasks in the closure. If you need to perform such tasks,
        /// dispatch to a background queue:
        ///
        ///     .onChange(of: scenePhase) { newScenePhase in
        ///         if newScenePhase == .background {
        ///             DispatchQueue.global(qos: .background).async {
        ///                 // ...
        ///             }
        ///         }
        ///     }
        ///
        /// The system passes the new value into the closure. If you need the old
        /// value, capture it in the closure.
        ///
        /// - Parameters:
        ///   - value: The value to check when determining whether to run the
        ///     closure. The value must conform to the
        ///     <doc://com.apple.documentation/documentation/Swift/Equatable>
        ///     protocol.
        ///   - action: A closure to run when the value changes. The closure
        ///     provides a single `newValue` parameter that indicates the changed
        ///     value.
        ///
        /// - Returns: A scene that triggers an action in response to a change.
    // 系统将新值传递到闭包。如果你需要老人值,在闭包中捕获它。
    ///
    ///  -参数:
    /// - value:决定是否运行时要检查的值
    /// 关闭。值必须符合
    ///<doc://com.apple.documentation/documentation/Swift/Equatable>
    ///协议。
    ///  - action:当值更改时运行的闭包。关闭
    /// 提供一个单独的“newValue”参数来指示更改的值
    ///值。
    ///
    /// -返回:一个场景,触发一个动作来响应一个变化。
        @inlinable public func onChange<V>(of value: V, perform action: @escaping (V) -> Void) -> some Scene where V : Equatable
    
    }
    
    @available(iOS 14.0, macOS 11.0, *)
    @available(tvOS, unavailable)
    @available(watchOS, unavailable)
    extension Scene {
    
        /// Adds commands to the scene.
        ///
        /// Commands are realized in different ways on different platforms. On
        /// macOS, the main menu uses the available command menus and groups to
        /// organize its main menu items. Each menu is represented as a top-level
        /// menu bar menu, and each command group has a corresponding set of menu
        /// items in one of the top-level menus, delimited by separator menu items.
        ///
        /// On iPadOS, commands with keyboard shortcuts are exposed in the shortcut
        /// discoverability HUD that users see when they hold down the Command (⌘)
        /// key.
    /// 向场景添加命令。
    /// 命令在不同的平台上以不同的方式实现。在
    // macOS中,主菜单使用可用的命令菜单和组来实现
    /// 组织其主要菜单项。每个菜单都表示为顶级菜单
    /// 菜单栏菜单,并且每个命令组都有对应的一组菜单
    /// 顶级菜单中的项,由分隔符菜单项分隔。
    /// 
    /// 在iPadOS上,带有键盘快捷键的命令会在快捷键中显示
    /// 可发现性住房和城市发展部,用户看到时按住命令(⌘)
    /// 关键。
    
        public func commands<Content>(@CommandsBuilder content: () -> Content) -> some Scene where Content : Commands
    
    }
    
    @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
    extension Scene {
    
        /// The default store used by `AppStorage` contained within the scene and
        /// its view content.
        ///
        /// If unspecified, the default store for a view hierarchy is
        /// `UserDefaults.standard`, but can be set a to a custom one. For example,
        /// sharing defaults between an app and an extension can override the
        /// default store to one created with `UserDefaults.init(suiteName:_)`.
        ///
        /// - Parameter store: The user defaults to use as the default
        ///   store for `AppStorage`.
    /// 场景中包含的“AppStorage”使用的默认存储
    /// 其视图内容。
    ///
    /// 如果未指定,视图层次结构的默认存储为
    /// UserDefaults。标准,但可以设置为自定义。例如,
    /// 在应用程序和扩展之间共享默认值可以覆盖
    /// 默认存储为用' UserDefaults.init(suiteName:_) '创建的。
    ///
    /// -参数存储:用户默认使用为默认值
    /// store for ' AppStorage '。
        public func defaultAppStorage(_ store: UserDefaults) -> some Scene
    
    }
    
    @available(iOS 14.0, macOS 11.0, *)
    @available(tvOS, unavailable)
    @available(watchOS, unavailable)
    extension Scene {
    
        /// Specifies a modifier to indicate if this Scene can be used
        /// when creating a new Scene for the received External Event.
        ///
        /// This modifier is only supported for WindowGroup Scene types.
        ///
        /// For DocumentGroups, the received External Event must have a URL
        /// for the DocumentGroup to be considered. (Either via openURL, or
        /// the webPageURL property of an NSUserActivity). The UTI for the URL
        /// is implicitly matched against the DocumentGroup's supported types.
        ///
        /// If the modifier evaluates to true, an instance of the
        /// Scene will be used.
        ///
        /// If the modifier evaluates to false, on macOS the Scene
        /// will not be used even if no other Scenes are available.
        /// This case is considered an error. On iOS, the first Scene
        /// specified in the body property for the App will be used.
        ///
        /// If no modifier is set, the Scene will be used if all
        /// other WindowGroups with a modifier evaluate to false.
        ///
        /// On platforms that only allow a single Window/Scene, this method is
        /// ignored.
        ///
        /// - Parameter matching: A Set of Strings that are checked to see
        /// if they are contained in the targetContentIdenfifier. The empty Set
        /// and empty Strings never match. The String value "*" always matches.
    /// 指定一个修饰符来指示这个场景是否可以使用
    /// 当为接收到的外部事件创建一个新场景时。
    ///
    /// 这个修改器只支持WindowGroup场景类型。
    ///
    /// 对于DocumentGroups,接收的外部事件必须有一个URL
    /// 要考虑的DocumentGroup。(要么通过openURL,要么
    /// NSUserActivity的webPageURL属性)。URL的UTI
    /// 与DocumentGroup支持的类型隐式匹配。
    ///
    /// 如果修饰符的值为真,则
    /// 场景将被使用。
    ///
    /// 如果修改器的值为false,在macOS场景中
    /// 将不会被使用,即使没有其他场景可用。
    /// 这种情况被认为是一个错误。在iOS上,第一个场景
    /// 在body属性中指定的应用程序将被使用。
    ///
    /// 如果没有设置修改器,场景将被使用
    /// 修改符为false的其他窗口组。
    ///
    /// 在只允许单个窗口/场景的平台上,此方法为
    /// 忽略。
    ///
    ///  -参数匹配:要检查的一组字符串
    /// 如果它们包含在targetContentIdenfifier中。空集
    /// 和空字符串永远不匹配。字符串值“*”总是匹配的。
    
      public func handlesExternalEvents(matching conditions: Set<String>) -> some Scene
    }
    

    SceneBuilder

    /// A function builder for composing a collection of scenes into a single
    /// composite scene.
    /// 一个函数构建器,用于将一组场景组合成单个场景
    /// 复合场景。
    @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
    @_functionBuilder public struct SceneBuilder {
    
        /// Passes a single scene written as a child scene through unmodified.
        public static func buildBlock<Content>(_ content: Content) -> Content where Content : Scene
    }
    
    @_functionBuilder
    

    @ViewBuilder

    // A custom parameter attribute that constructs views from closures.
    ///
    /// You typically use ``ViewBuilder`` as a parameter attribute for child
    /// view-producing closure parameters, allowing those closures to provide
    /// multiple child views. For example, the following `contextMenu` function
    /// accepts a closure that produces one or more views via the view builder.
    /// 从闭包构造视图的自定义参数属性。
    ///
    /// 你通常使用' ' ViewBuilder ' '作为child的参数属性
    /// 视图生成闭包参数,允许这些闭包提供
    /// 多个子视图。例如,下面的“contextMenu”函数
    /// 接受一个闭包,该闭包通过视图生成器生成一个或多个视图。
    ///
    ///     func contextMenu<MenuItems : View>(
    ///         @ViewBuilder menuItems: () -> MenuItems
    ///     ) -> some View
    ///
    /// Clients of this function can use multiple-statement closures to provide
    /// several child views, as shown in the following example:
    ///
    /// 该函数的客户端可以使用多语句闭包来提供
    /// 几个子视图,如下面的例子所示:
    ///     myView.contextMenu {
    ///         Text("Cut")
    ///         Text("Copy")
    ///         Text("Paste")
    ///         if isSymbol {
    ///             Text("Jump to Definition")
    ///         }
    ///     }
    ///
    
    @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
    @_functionBuilder public struct ViewBuilder {
    
        /// Builds an empty view from a block containing no statements.
        public static func buildBlock() -> EmptyView
    
        /// Passes a single view written as a child view through unmodified.
        ///
        /// An example of a single view written as a child view is
        /// `{ Text("Hello") }`.
        public static func buildBlock<Content>(_ content: Content) -> Content where Content : View
    }
    
    @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
    extension ViewBuilder {
    
        /// Provides support for “if” statements in multi-statement closures,
        /// producing an optional view that is visible only when the condition
        /// evaluates to `true`.
        public static func buildIf<Content>(_ content: Content?) -> Content? where Content : View
    
        /// Provides support for "if" statements in multi-statement closures,
        /// producing conditional content for the "then" branch.
        public static func buildEither<TrueContent, FalseContent>(first: TrueContent) -> _ConditionalContent<TrueContent, FalseContent> where TrueContent : View, FalseContent : View
    
        /// Provides support for "if-else" statements in multi-statement closures,
        /// producing conditional content for the "else" branch.
        public static func buildEither<TrueContent, FalseContent>(second: FalseContent) -> _ConditionalContent<TrueContent, FalseContent> where TrueContent : View, FalseContent : View
    }
    
    @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
    extension ViewBuilder {
    
        /// Provides support for "if" statements with `#available()` clauses in
        /// multi-statement closures, producing conditional content for the "then"
        /// branch, i.e. the conditionally-available branch.
        public static func buildLimitedAvailability<Content>(_ content: Content) -> AnyView where Content : View
    }
    

    @CommandsBuilder

    /// Constructs command sets from multi-expression closures. Like `ViewBuilder`,
    /// it supports up to ten expressions in the closure body.
    /// 从多表达式闭包构造命令集。像“ViewBuilder”,
    /// 它在闭包体中最多支持10个表达式。
    @available(iOS 14.0, macOS 11.0, *)
    @available(tvOS, unavailable)
    @available(watchOS, unavailable)
    @_functionBuilder public struct CommandsBuilder {
    
        /// Builds an empty command set from an block containing no statements.
        public static func buildBlock() -> EmptyCommands
    
        /// Passes a single command group written as a child group through
        /// modified.
        public static func buildBlock<Content>(_ content: Content) -> Content where Content : Commands
    }
    
    @available(iOS 14.0, macOS 11.0, *)
    @available(tvOS, unavailable)
    @available(watchOS, unavailable)
    extension CommandsBuilder {
        public static func buildBlock<C0, C1>(_ c0: C0, _ c1: C1) -> some Commands where C0 : Commands, C1 : Commands
    }
    

    相关文章

      网友评论

          本文标题:SwiftUI 学习 Scene

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