EnvironmentValues
可以看前面章节Swift学习,可以了解到scenePhase的用途。
与属性注解访问方式对应的,结构体实现方式为EnvironmentValues。
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
public struct EnvironmentValues : CustomStringConvertible {}
文档完整解释
EnvironmentValues 文件开头讲述了如何使用
1.常规使用方式(内部属性变量、外部使用)
1.1 内部属性变量:
@Environment(\.locale) var locale: Locale
1.2 外部使用
MyView().environment(\.lineLimit, 2)
2.使用专门的视图修改器,直接确保新值在显示视图时向上转播到显示容器。
/// MyView()
/// .lineLimit(2)
/// MyView()
/// .popover(isPresented: $isPopped) {
/// PopoverContent()
/// // 必须使用'View/preferredColorScheme(_:) '修饰符,
/// 而不置EnvironmentValues/colorScheme.
/// .preferredColorScheme(.dark)
/// }
3.自定义环境变量
/// private struct MyEnvironmentKey: EnvironmentKey {
/// static let defaultValue: String = "Default value"
/// }
///
/// extension EnvironmentValues {
/// var myCustomValue: String {
/// get { self[MyEnvironmentKey.self] }
/// set { self[MyEnvironmentKey.self] = newValue }
/// }
/// }
///
/// extension View {
/// func myCustomValue(_ myCustomValue: String) -> some View {
/// environment(\.myCustomValue, myCustomValue)
/// }
/// }
开头详情如下
/// A collection of environment values propagated through a view hierarchy.
///
/// SwiftUI exposes a collection of values to your app's views in an
/// `EnvironmentValues` structure. To read a value from the structure,
/// declare a property using the ``Environment`` property wrapper and
/// specify the value's key path. For example, you can read the current locale:
///
/// @Environment(\.locale) var locale: Locale
///
/// Use the property you declare to dynamically control a view's layout.
/// SwiftUI automatically sets or updates many environment values, like
/// ``EnvironmentValues/pixelLength``, ``EnvironmentValues/scenePhase``, or
/// ``EnvironmentValues/locale``, based on device characteristics, system state,
/// or user settings. For others, like ``EnvironmentValues/lineLimit``, SwiftUI
/// provides a reasonable default value.
///
/// You can set or override some values using the ``View/environment(_:_:)``
/// view modifier:
///
/// MyView()
/// .environment(\.lineLimit, 2)
///
/// The value that you set affects the environment for the view that you modify
/// --- including its descendants in the view hierarchy --- but only up to the
/// point where you apply a different environment modifier.
///
/// SwiftUI provides dedicated view modifiers for setting some values, which
/// typically makes your code easier to read. For example, rather than setting
/// the ``EnvironmentValues/lineLimit`` value directly, as in the previous
/// example, you should instead use the ``View/lineLimit(_:)`` modifier:
///
/// MyView()
/// .lineLimit(2)
///
/// In some cases, using a dedicated view modifier provides additional
/// functionality. For example, you must use the
/// ``View/preferredColorScheme(_:)`` modifier rather than setting
/// ``EnvironmentValues/colorScheme`` directly to ensure that the new
/// value propagates up to the presenting container when presenting a view
/// like a popover:
///
/// MyView()
/// .popover(isPresented: $isPopped) {
/// PopoverContent()
/// .preferredColorScheme(.dark)
/// }
///
/// Create custom environment values by defining a type that
/// conforms to the ``EnvironmentKey`` protocol, and then extending the
/// environment values structure with a new property. Use your key to get and
/// set the value, and provide a dedicated modifier for clients to use when
/// setting the value:
///
/// private struct MyEnvironmentKey: EnvironmentKey {
/// static let defaultValue: String = "Default value"
/// }
///
/// extension EnvironmentValues {
/// var myCustomValue: String {
/// get { self[MyEnvironmentKey.self] }
/// set { self[MyEnvironmentKey.self] = newValue }
/// }
/// }
///
/// extension View {
/// func myCustomValue(_ myCustomValue: String) -> some View {
/// environment(\.myCustomValue, myCustomValue)
/// }
/// }
///
/// Clients of your value then access the value in the usual way, reading it
/// with the ``Environment`` property wrapper, and setting it with the
/// `myCustomValue` view modifier.
EnvironmentValues 结构体
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
public struct EnvironmentValues : CustomStringConvertible {
/// Creates an environment values instance.
///
/// You don't typically create an instance of ``EnvironmentValues``
/// directly. Doing so would provide access only to default values that
/// don't update based on system settings or device characteristics.
/// Instead, you rely on an environment values' instance
/// that SwiftUI manages for you when you use the ``Environment``
/// property wrapper and the ``View/environment(_:_:)`` view modifier.
public init()
/// Accesses the environment value associated with a custom key.
///
/// Create custom environment values by defining a key
/// that conforms to the ``EnvironmentKey`` protocol, and then using that
/// key with the subscript operator of the ``EnvironmentValues`` structure
/// to get and set a value for that key:
///
/// private struct MyEnvironmentKey: EnvironmentKey {
/// static let defaultValue: String = "Default value"
/// }
///
/// extension EnvironmentValues {
/// var myCustomValue: String {
/// get { self[MyEnvironmentKey.self] }
/// set { self[MyEnvironmentKey.self] = newValue }
/// }
/// }
///
/// You use custom environment values the same way you use system-provided
/// values, setting a value with the ``View/environment(_:_:)`` view
/// modifier, and reading values with the ``Environment`` property wrapper.
/// You can also provide a dedicated view modifier as a convenience for
/// setting the value:
///
/// extension View {
/// func myCustomValue(_ myCustomValue: String) -> some View {
/// environment(\.myCustomValue, myCustomValue)
/// }
/// }
///
public subscript<K>(key: K.Type) -> K.Value where K : EnvironmentKey
/// A string that represents the contents of the environment values
/// instance.
public var description: String { get }
}
相关拓展如下:
extension EnvironmentValues {
/// A Boolean value that determines whether the view hierarchy has
/// auto-correction enabled.
///
/// When the value is `nil`, SwiftUI uses the system default. The default
/// value is `nil`.
@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
@available(watchOS, unavailable)
public var disableAutocorrection: Bool?
}
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
extension EnvironmentValues {
/// The preferred size of the content.
///
/// The default value is ``ContentSizeCategory/large``.
public var sizeCategory: ContentSizeCategory
}
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
extension EnvironmentValues {
public var managedObjectContext: NSManagedObjectContext
}
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
extension EnvironmentValues {
/// The undo manager used to register a view's undo operations.
///
/// This value is `nil` when the environment represents a context that
/// doesn't support undo and redo operations. You can skip registration of
/// an undo operation when this value is `nil`.
public var undoManager: UndoManager? { get }
}
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
extension EnvironmentValues {
/// The layout direction associated with the current environment.
///
/// Use this value to determine whether the environment uses a left-to-right
/// or right-to-left orientation.
public var layoutDirection: LayoutDirection
}
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
extension EnvironmentValues {
/// The current redaction reasons applied to the view hierarchy.
public var redactionReasons: RedactionReasons
}
@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
}
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
extension EnvironmentValues {
/// The default minimum height of a row in a list.
public var defaultMinListRowHeight: CGFloat
/// The default minimum height of a header in a list.
///
/// When this value is `nil`, the system chooses the appropriate height. The
/// default is `nil`.
public var defaultMinListHeaderHeight: CGFloat?
}
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
extension EnvironmentValues {
/// A Boolean value that indicates whether the view associated with this
/// environment allows user interaction.
///
/// The default value is `true`.
public var isEnabled: Bool
}
extension EnvironmentValues {
/// Returns whether the nearest focusable ancestor has focus.
///
/// If there is no focusable ancestor, the value is `false`.
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
public var isFocused: Bool { get }
}
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
extension EnvironmentValues {
/// The default font of this environment.
public var font: Font?
/// The display scale of this environment.
public var displayScale: CGFloat
/// The image scale for this environment.
@available(macOS 11.0, *)
public var imageScale: Image.Scale
/// The size of a pixel on the screen.
///
/// This value is equal to `1` divided by
/// ``EnvironmentValues/displayScale``.
public var pixelLength: CGFloat { get }
/// The font weight to apply to text.
///
/// This value reflects the value of the Bold Text display setting found in
/// the Accessibility settings.
public var legibilityWeight: LegibilityWeight?
/// The current locale that views should use.
public var locale: Locale
/// The current calendar that views should use when handling dates.
public var calendar: Calendar
/// The current time zone that views should use when handling dates.
public var timeZone: TimeZone
/// The color scheme of this environment.
///
/// When writing custom drawing code that depends on the current color
/// scheme, you should also consider the
/// ``EnvironmentValues/colorSchemeContrast`` property. You can specify
/// images and colors in asset catalogs according to either the
/// ``ColorScheme/light`` or ``ColorScheme/dark`` color scheme, as well as
/// standard or increased contrast. The correct image or color displays
/// automatically for the current environment.
///
/// You only need to check `colorScheme` and
/// ``EnvironmentValues/colorSchemeContrast`` for custom drawing if the
/// differences go beyond images and colors.
///
/// Setting the `colorScheme` environment value directly is an advanced use
/// case, as it changes the color scheme of the contained views but *not* of
/// the container. Instead, consider using the
/// ``View/preferredColorScheme(_:)`` modifier, which propagates to the
/// presentation containing the view.
public var colorScheme: ColorScheme
/// The contrast associated with the color scheme of this environment.
public var colorSchemeContrast: ColorSchemeContrast { get }
}
@available(iOS 13.0, *)
@available(macOS, unavailable)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
extension EnvironmentValues {
/// The horizontal size class of this environment.
@available(macOS, unavailable)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
public var horizontalSizeClass: UserInterfaceSizeClass?
/// The vertical size class of this environment.
@available(macOS, unavailable)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
public var verticalSizeClass: UserInterfaceSizeClass?
}
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
extension EnvironmentValues {
/// A Boolean value that indicates whether the user has enabled an assistive
/// technology.
public var accessibilityEnabled: Bool
}
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
extension EnvironmentValues {
/// Whether the system preference for Differentiate without Color is enabled.
///
/// If this is true, UI should not convey information using color alone
/// and instead should use shapes or glyphs to convey information.
public var accessibilityDifferentiateWithoutColor: Bool { get }
/// Whether the system preference for Reduce Transparency is enabled.
///
/// If this property's value is true, UI (mainly window) backgrounds should
/// not be semi-transparent; they should be opaque.
public var accessibilityReduceTransparency: Bool { get }
/// Whether the system preference for Reduce Motion is enabled.
///
/// If this property's value is true, UI should avoid large animations,
/// especially those that simulate the third dimension.
public var accessibilityReduceMotion: Bool { get }
/// Whether the system preference for Invert Colors is enabled.
///
/// If this property's value is true then the display will be inverted.
/// In these cases it may be needed for UI drawing to be adjusted to in
/// order to display optimally when inverted.
public var accessibilityInvertColors: Bool { get }
/// Whether the system preference for Show Button Shapes is enabled.
///
/// If this property's value is true, interactive custom controls
/// such as buttons should be drawn in such a way that their edges
/// and borders are clearly visible.
public var accessibilityShowButtonShapes: Bool { get }
}
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
extension EnvironmentValues {
/// Opens a URL using the appropriate system service.
public var openURL: OpenURLAction { get }
}
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
extension EnvironmentValues {
/// A value that indicates how text instance aligns its lines when the
/// content wraps or contains newlines.
///
/// Use alignment parameters on a parent view to align ``Text`` with respect
/// to its parent. Because the horizontal bounds of ``TextField`` never
/// exceed its graphical extent, this value has little to no effect on
/// single-line text.
public var multilineTextAlignment: TextAlignment
/// A value that indicates how the layout truncates the last line of text to
/// fit into the available space.
///
/// The default value is ``Text/TruncationMode/tail``.
public var truncationMode: Text.TruncationMode
/// The distance in points between the bottom of one line fragment and the
/// top of the next.
///
/// This value is always nonnegative.
public var lineSpacing: CGFloat
/// A Boolean value that indicates whether inter-character spacing should
/// tighten to fit the text into the available space.
///
/// The default value is `false`.
public var allowsTightening: Bool
/// The maximum number of lines that text can occupy in a view.
///
/// The maximum number of lines is `1` if the value is less than `1`. If the
/// value is `nil`, the text uses as many lines as required. The default is
/// `nil`.
public var lineLimit: Int?
/// The minimum permissible proportion to shrink the font size to fit
/// the text into the available space.
///
/// In the example below, a label with a `minimumScaleFactor` of `0.5`
/// draws its text in a font size as small as half of the actual font if
/// needed to fit into the space next to the text input field:
///
/// HStack {
/// Text("This is a very long label:")
/// .lineLimit(1)
/// .minimumScaleFactor(0.5)
/// TextField("My Long Text Field", text: $myTextField)
/// .frame(width: 250, height: 50, alignment: .center)
/// }
///
/// ![A screenshot showing the effects of setting the minimumScaleFactor on
/// the text in a view](SwiftUI-view-minimumScaleFactor.png)
///
/// You can set the minimum scale factor to any value greater than `0` and
/// less than or equal to `1`. The default value is `1`.
///
/// SwiftUI uses this value to shrink text that doesn't fit in a view when
/// it's okay to shrink the text. For example, a label with a
/// `minimumScaleFactor` of `0.5` draws its text in a font size as small as
/// half the actual font if needed.
public var minimumScaleFactor: CGFloat
/// A stylistic override to transform the case of `Text` when displayed,
/// using the environment's locale.
///
/// The default value is `nil`, displaying the `Text` without any case
/// changes.
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
public var textCase: Text.Case?
}
@available(iOS 13.0, tvOS 13.0, *)
@available(macOS, unavailable)
@available(watchOS, unavailable)
extension EnvironmentValues {
/// The mode indicating whether the user can edit the contents of a view
/// associated with this environment.
@available(macOS, unavailable)
@available(watchOS, unavailable)
public var editMode: Binding<EditMode>?
}
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
extension EnvironmentValues {
/// A binding to the current presentation mode of the view associated with
/// this environment.
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
public var presentationMode: Binding<PresentationMode> { get }
}
@EnvironmentObject
结构体声明
/// 父或继承者的可观察对象的属性包装类型
///
/// 环境对象未使用会使当前视图失效
/// 对象的变化。如果您将属性声明为环境对象,请确保
/// 通过调用其在视图上设置相应的模型对象
/// “视图/ environmentObject _:“修饰符。
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
@frozen @propertyWrapper public struct EnvironmentObject<ObjectType> : DynamicProperty where ObjectType : ObservableObject {
@frozen
@propertyWrapper
结构体内部结构
@frozen @propertyWrapper public struct EnvironmentObject<ObjectType> : DynamicProperty where ObjectType : ObservableObject {
/// A wrapper of the underlying environment object that can create bindings
/// to its properties using dynamic member lookup.
/// 可以创建绑定的底层环境对象的包装器
/// 使用动态成员查找来获取其属性。
@dynamicMemberLookup @frozen public struct Wrapper {
/// Returns a binding to the resulting value of a given key path.
///
/// 返回对给定键路径的结果值的绑定。
/// - Parameter keyPath: A key path to a specific resulting value.
///
/// - Returns: A new binding.
public subscript<Subject>(dynamicMember keyPath: ReferenceWritableKeyPath<ObjectType, Subject>) -> Binding<Subject> { get }
}
/// The underlying value referenced by the environment object.
///
/// This property provides primary access to the value's data. However, you
/// don't access `wrappedValue` directly. Instead, you use the property
/// variable created with the ``EnvironmentObject`` attribute.
///
/// When a mutable value changes, the new value is immediately available.
/// However, a view displaying the value is updated asynchronously and may
/// not show the new value immediately.
/// 环境对象引用的基础值。
///
/// 此属性提供对值数据的主要访问。然而,你
/// 不要直接访问' wrappedValue '。相反,您可以使用该属性
/// 变量由“EnvironmentObject”属性创建。
///
/// 当可变值更改时,新值立即可用。
/// 然而,显示值的视图是异步更新的,并且可以
/// 不立即显示新值。
@inlinable public var wrappedValue: ObjectType { get }
/// A projection of the environment object that creates bindings to its
/// properties using dynamic member lookup.
///
/// Use the projected value to pass an environment object down a view
/// hierarchy.
/// 环境对象的投影,该投影创建到其的绑定
/// 属性使用动态成员查找。
///
/// 使用投影值将环境对象传递到视图中
/// 层次结构。
public var projectedValue: EnvironmentObject<ObjectType>.Wrapper { get }
/// Creates an environment object.
public init()
}
@dynamicMemberLookup
@inlinable
ReferenceWritableKeyPath<ObjectType, Subject>
Binding<Subject>
@StateObject
开头描述
1./// 一个实例化可观察对象的属性包装器类型。
///
/// 在' ' SwiftUI/View ' ', ' ' SwiftUI/App ' ',或
/// 通过将“@StateObject”属性应用到一个属性来实现“SwiftUI/Scene”
/// 声明,并提供符合的初始值
///<doc://com.apple.documentation/documentation/Combine/ObservableObject>
/// 协议:
///
/// @StateObject var model = DataModel()
///
/// 对于每个实例,SwiftUI只创建该对象的一个新实例
/// 声明对象的结构。属性的
/// 可观察对象变化,SwiftUI更新任何视图依赖的部分
/// 关于这些属性:
///
/// Text(model.title) //每当“title”发生变化时更新视图。
2./// 您可以将状态对象传递到具有
/// ‘SwiftUI ObservedObject“属性。您也可以添加对象
方法应用到视图层次结构的环境中
/// SwiftUI /视图/ environmentObject(_:)的修饰符:
///
/// ContentView ()
/// .environmentObject(模型)
///
/// 如果您像上面代码中所示那样创建了一个环境对象,那么您可以这样做
/// 读取' ContentView '内部的对象或它的任何后代
/// 使用' SwiftUI/EnvironmentObject ' '属性:
///
/// @EnvironmentObject var model: DataModel
3./// 方法获取一个'SwiftUI/Binding'到状态对象的一个属性"美元"(简书符号识别格式化有误)操作符
/// 当您想要创建双向连接时,请使用绑定对象的属性之一。
/// 例如,你可以让a控件中存储的布尔值isEnabled
/// 模型:
/// Toggle("Enabled", isOn: $model.isEnabled)
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
@frozen @propertyWrapper public struct StateObject<ObjectType> : DynamicProperty where ObjectType : ObservableObject {}
@frozen
@propertyWrapper
结构体内容结构
@frozen @propertyWrapper public struct StateObject<ObjectType> : DynamicProperty where ObjectType : ObservableObject {
/// Creates a new state object with an initial wrapped value.
/// 创建一个带有初始包装值的新状态对象。
///
/// You don’t call this initializer directly. Instead, declare a property
/// with the `@StateObject` attribute in a ``SwiftUI/View``,
/// ``SwiftUI/App``, or ``SwiftUI/Scene``, and provide an initial value:
///
/// struct MyView: View {
/// @StateObject var model = DataModel()
///
/// // ...
/// }
///
/// SwiftUI creates only one instance of the state object for each
/// container instance that you declare. In the code above, SwiftUI
/// creates `model` only the first time it initializes a particular instance
/// of `MyView`. On the other hand, each different instance of `MyView`
/// receives a distinct copy of the data model.
///
/// - Parameter thunk: An initial value for the state object.
/// SwiftUI只为每个state对象创建一个实例
/// 您声明的容器实例。在上面的代码中,SwiftUI
/// 仅在第一次初始化特定实例时创建' model '
/// “MyView”。另一方面,MyView的每个不同实例
/// 接收数据模型的不同副本。
@inlinable public init(wrappedValue thunk: @autoclosure @escaping () -> ObjectType)
/// The underlying value referenced by the state object.
///
/// The wrapped value property provides primary access to the value's data.
/// However, you don't access `wrappedValue` directly. Instead, use the
/// property variable created with the `@StateObject` attribute:
///
/// @StateObject var contact = Contact()
///
/// var body: some View {
/// Text(contact.name) // Accesses contact's wrapped value.
/// }
///
/// When you change a property of the wrapped value, you can access the new
/// value immediately. However, SwiftUI updates views displaying the value
/// asynchronously, so the user interface might not update immediately.
public var wrappedValue: ObjectType { get }
/// A projection of the state object that creates bindings to its
/// properties.
/// 状态对象的投影,该状态对象创建到其的绑定
/// 属性。
///
/// Use the projected value to pass a binding value down a view hierarchy.
/// To get the projected value, prefix the property variable with `$`. For
/// example, you can get a binding to a model's `isEnabled` Boolean so that
/// a ``SwiftUI/Toggle`` view can control the value:
/// 使用投影值向视图层次结构传递绑定值。
/// 要获得投影值,请在属性变量前加上' $ '。为
/// 例如,您可以获得一个绑定到模型的' isEnabled '布尔值,从而使
/// 一个' ' SwiftUI/ ' Toggle ' ' view可以控制该值:
///
/// struct MyView: View {
/// @StateObject var model = DataModel()
///
/// var body: some View {
/// Toggle("Enabled", isOn: $model.isEnabled)
/// }
/// }
public var projectedValue: ObservedObject<ObjectType>.Wrapper { get }
}
@State
开头作用描述
/// 一个可以读写由SwiftUI管理的值的属性包装器类型。
///
/// SwiftUI管理您声明为状态的任何属性的存储。当
状态值更改时,视图将使其外观无效并重新计算
/// 身体。使用国家作为一个给定观点的唯一真相来源。
///
/// 一个' State '实例不是值本身;它是一种阅读和阅读的方式
/// 写入值。要访问状态的基础值,请使用状态的变量
/// name,返回' ' State/wrappedValue ' '属性值。
///
/// 只能从视图的主体内部访问状态属性
/// 它调用的方法。因此,将状态属性声明为
/// private,防止视图的客户端访问它们。是安全的
/// 改变任何线程的状态属性。
///
/// 要将状态属性传递给视图层次结构中的另一个视图,请使用
///带有“美元”前缀操作符的变量名。属性的绑定
state /projectedValue属性中的state属性。例如,在
/// 下面的代码示例' PlayerView '传递它的state属性
/// ' isPlaying '到' PlayButton '使用' ”美元“(简书符号解析失败 text parse error)isPlaying ':
///
/// struct PlayerView: View {
/// var episode: Episode
/// @State private var isPlaying: Bool = false
///
/// var body: some View {
/// VStack {
/// Text(episode.title)
/// Text(episode.showTitle)
/// PlayButton(isPlaying: $isPlaying)
/// }
/// }
/// }
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
@frozen @propertyWrapper public struct State<Value> : DynamicProperty {}
相关扩展
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
extension State where Value : ExpressibleByNilLiteral {
/// Creates a state without an initial value.
@inlinable public init()
}
@Binding
开头使用场景描述
/// A property wrapper type that can read and write a value owned by a source of
/// truth.
/// 属性包装器类型,它可以读写源所拥有的正确的值。
///
/// Use a binding to create a two-way connection between a property that stores
/// data, and a view that displays and changes the data. A binding connects a
/// property to a source of truth stored elsewhere, instead of storing data
/// directly. For example, a button that toggles between play and pause can
/// create a binding to a property of its parent view using theBinding
/// property wrapper.
/// 使用绑定在存储的属性之间创建双向连接
/// data,以及显示和更改数据的视图。A结合连接A
/// 属性将一个来源的真相存储在别处,而不是存储数据
/// 直接。例如,一个在播放和暂停之间切换的按钮就可以
/// 使用“binding”创建一个绑定到其父视图的属性
/ / 包装/属性。
///
/// struct PlayButton: View {
/// @Binding var isPlaying: Bool
///
/// var body: some View {
/// Button(action: {
/// self.isPlaying.toggle()
/// }) {
/// Image(systemName: isPlaying ? "pause.circle" : "play.circle")
/// }
/// }
/// }
2./// The parent view declares a property to hold the playing state, using the
///State
property wrapper to indicate that this property is the value's
/// source of truth.
/// 父视图声明一个属性来保存播放状态
/// ' State '属性包装器来指示此属性是值的真相的来源。
///
/// struct PlayerView: View {
/// var episode: Episode
/// @State private var isPlaying: Bool = false
///
/// var body: some View {
/// VStack {
/// Text(episode.title)
/// Text(episode.showTitle)
/// PlayButton(isPlaying: $isPlaying)
/// }
/// }
/// }
///
/// WhenPlayerView
initializesPlayButton
, it passes a binding of its state
/// property into the button's binding property. Applying the美元(简书符号解析错误)
prefix to a
/// property wrapped value returns itsState/projectedValue
, which for a
/// state property wrapper returns a binding to the value.
///
/// Whenever the user taps thePlayButton
, thePlayerView
updates its
///isPlaying
state.
// 当' PlayerView '初始化' PlayButton '时,它传递一个其状态的绑定
/// 属性添加到按钮的绑定属性中。将“美元(简书符号解析失败)”前缀应用于a
/// 属性包装的值返回它的' ' State/projectedValue ' ',对于
/// state属性包装器返回值的绑定。
/// 使用场景:
/// 每当用户点击“播放按钮”时,“PlayerView”就会更新它的
/// “isPlaying”状态。
结构体声明
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
@frozen @propertyWrapper @dynamicMemberLookup public struct Binding<Value> {}
@dynadmicMemberLookup
@frozen
@propertyWrapper
结构体内部
@frozen @propertyWrapper @dynamicMemberLookup public struct Binding<Value> {
/// The binding's transaction.
///
/// The transaction captures the information needed to update the view when
/// the binding value changes.
public var transaction: Transaction
/// Creates a binding with closures that read and write the binding value.
///
/// - Parameters:
/// - get: A closure that retrieves the binding value. The closure has no
/// parameters, and returns a value.
/// - set: A closure that sets the binding value. The closure has the
/// following parameter:
/// - newValue: The new value of the binding value.
public init(get: @escaping () -> Value, set: @escaping (Value) -> Void)
/// Creates a binding with a closure that reads from the binding value, and
/// a closure that applies a transaction when writing to the binding value.
///
/// - Parameters:
/// - get: A closure to retrieve the binding value. The closure has no
/// parameters, and returns a value.
/// - set: A closure to set the binding value. The closure has the
/// following parameters:
/// - newValue: The new value of the binding value.
/// - transaction: The transaction to apply when setting a new value.
public init(get: @escaping () -> Value, set: @escaping (Value, Transaction) -> Void)
/// Creates a binding with an immutable value.
///
/// Use this method to create a binding to a value that cannot change.
/// This can be useful when using a ``PreviewProvider`` to see how a view
/// represents different values.
///
/// // Example of binding to an immutable value.
/// PlayButton(isPlaying: Binding.constant(true))
///
/// - Parameter value: An immutable value.
public static func constant(_ value: Value) -> Binding<Value>
/// The underlying value referenced by the binding variable.
///
/// This property provides primary access to the value's data. However, you
/// don't access `wrappedValue` directly. Instead, you use the property
/// variable created with the `@Binding` attribute. For instance, in the
/// following code example the binding variable `isPlaying` returns the
/// value of `wrappedValue`:
///
/// struct PlayButton: View {
/// @Binding var isPlaying: Bool
///
/// var body: some View {
/// Button(action: {
/// self.isPlaying.toggle()
/// }) {
/// Image(systemName: isPlaying ? "pause.circle" : "play.circle")
/// }
/// }
/// }
///
/// When a mutable binding value changes, the new value is immediately
/// available. However, updates to a view displaying the value happens
/// asynchronously, so the view may not show the change immediately.
public var wrappedValue: Value { get nonmutating set }
/// A projection of the binding value that returns a binding.
///
/// Use the projected value to pass a binding value down a view hierarchy.
/// To get the `projectedValue`, prefix the property variable with `$`. For
/// example, in the following code example `PlayerView` projects a binding
/// of the state property `isPlaying` to the `PlayButton` view using
/// `$isPlaying`.
///
/// struct PlayerView: View {
/// var episode: Episode
/// @State private var isPlaying: Bool = false
///
/// var body: some View {
/// VStack {
/// Text(episode.title)
/// Text(episode.showTitle)
/// PlayButton(isPlaying: $isPlaying)
/// }
/// }
/// }
public var projectedValue: Binding<Value> { get }
/// Returns a binding to the resulting value of a given key path.
///
/// - Parameter keyPath: A key path to a specific resulting value.
///
/// - Returns: A new binding.
public subscript<Subject>(dynamicMember keyPath: WritableKeyPath<Value, Subject>) -> Binding<Subject> { get }
}
相关扩展
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
extension Binding {
/// Creates a binding by projecting the base value to an optional value.
///
/// - Parameter base: A value to project to an optional value.
public init<V>(_ base: Binding<V>) where Value == V?
/// Creates a binding by projecting the base value to an unwrapped value.
///
/// - Parameter base: A value to project to an unwrapped value.
///
/// - Returns: A new binding or `nil` when `base` is `nil`.
public init?(_ base: Binding<Value?>)
/// Creates a binding by projecting the base value to a hashable value.
///
/// - Parameters:
/// - base: A `Hashable` value to project to an `AnyHashable` value.
public init<V>(_ base: Binding<V>) where Value == AnyHashable, V : Hashable
}
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
extension Binding {
/// Specifies a transaction for the binding.
///
/// - Parameter transaction : An instance of a ``Transaction``.
///
/// - Returns: A new binding.
public func transaction(_ transaction: Transaction) -> Binding<Value>
/// Specifies an animation to perform when the binding value changes.
///
/// - Parameter animation: An animation sequence performed when the binding
/// value changes.
///
/// - Returns: A new binding.
public func animation(_ animation: Animation? = .default) -> Binding<Value>
}
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
extension Binding : DynamicProperty {
}
@FocusedBinding
**开头使用描述
///一个便于观察和自动展开的属性包装器
///来自聚焦视图或其祖先之一的状态绑定。
///
///如果多个视图使用相同的键发布绑定,则包装属性
///将反映来自最接近焦点的视图的绑定值。
结构体声明
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
@propertyWrapper public struct FocusedBinding<Value> : DynamicProperty {}
待举例
@FocusedValue
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
@propertyWrapper public struct FocusedValue<Value> : DynamicProperty {}
待举例
@publish
A publisher for properties marked with the @Published attribute.
publisher :Declares that a type can transmit a sequence of values over time.
(声明类型可以随时间传输一系列值。)
发布者将元素交付给一个或多个订阅者实例。订阅服务器的输入和故障关联类型必须与发布服务器声明的输出和故障类型匹配。发布者实现了receive(subscriber:)方法来接受订阅者。
struct Publisher
/// final class Settings: ObservableObject {
/// **@Published var alignmentGuideColor =
/// Color(.sRGB, red: 0.98, green: 0.9, blue: 0.2)**
/// }
///
/// struct FormattingControls: View {
/// @State private var settings = Settings()
///
/// var body: some View {
/// VStack {
/// // Other formatting controls.
/// ColorPicker("Alignment Guides",
/// selection: $settings.alignmentGuideColor
/// )
/// }
/// }
/// }
待举例