使用 @Namespace 为 .matchedGeometryEffect 自定义初始化
Posted
技术标签:
【中文标题】使用 @Namespace 为 .matchedGeometryEffect 自定义初始化【英文标题】:Custom init with @Namespace for .matchedGeometryEffect 【发布时间】:2020-09-04 17:46:47 【问题描述】:我正在尝试将带有 @Namespace
属性的自定义视图结构传递给父视图。
由于父母将提供Namespace
,因此我使用自定义init
。
当我使用类似于@Binding
自定义初始化的语法时,Xcode 会强制我在初始化自定义视图时使用包装器。这反过来会杀死我的.matchedGeometryEffect
。
struct MyView<Content: View>: View
@Binding var matched: Bool
@Namespace var nspace
let content: Content
init(matched: Binding<Bool>,
nspace: Namespace,
@ViewBuilder content: @escaping () -> Content
)
self._matched = matched
self._nspace = nspace
self.content = content()
var body: some View
...
似乎可行的是使用var nspace: Namespace.ID
而不是@Namespace var nspace
然后:
struct MyView<Content: View>: View
@Binding var matched: Bool
var nspace: Namespace.ID
let content: Content
init(matched: Binding<Bool>,
nspace: Namespace.ID,
@ViewBuilder content: @escaping () -> Content
)
self._matched = matched
self.nspace = nspace
self.content = content()
var body: some View
...
这会在其他地方造成麻烦吗?有没有更好的办法?
【问题讨论】:
Namespace.ID 与它应该传递的完全一样,例如,参见 ***.com/a/63131527/12299030 【参考方案1】:这会在其他地方造成麻烦吗?有没有更好的办法?
这不是更糟/更好,它是唯一正确的方法。让我们看看 API:
Namespace.ID
是用于标识匹配效果的命名空间的值
/// A dynamic property type that allows access to a namespace defined
/// by the persistent identity of the object containing the property
/// (e.g. a view).
@available(ios 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
@frozen @propertyWrapper public struct Namespace : DynamicProperty
@inlinable public init()
public var wrappedValue: Namespace.ID get // << here !!
正如所见
/// - namespace: The namespace in which defines the `id`. New
/// namespaces are created by adding an `@Namespace()` variable
/// to a ``View`` type and reading its value in the view's body
/// method.
/// - properties: The properties to copy from the source view.
/// - anchor: The relative location in the view used to produce
/// its shared position value.
/// - isSource: True if the view should be used as the source of
/// geometry for other views in the group.
///
/// - Returns: A new view that defines an entry in the global
/// database of views synchronizing their geometry.
///
@inlinable public func matchedGeometryEffect<ID>(id: ID,
in namespace: Namespace.ID, // << here !!
properties: MatchedGeometryProperties = .frame, anchor: UnitPoint = .center, isSource: Bool = true) -> some View where ID : Hashable
【讨论】:
以上是关于使用 @Namespace 为 .matchedGeometryEffect 自定义初始化的主要内容,如果未能解决你的问题,请参考以下文章