Init personalizado com @Namespace para .matchedGeometryEffect

Sep 05 2020

Estou tentando passar uma estrutura de visualização personalizada com uma @Namespacepropriedade para uma .matchedGeometryEffectvisualização pai.

Já que o pai estará fornecendo o Namespaceestou usando um costume init.

Quando uso a sintaxe análoga à @Bindinginicialização personalizada, o Xcode me força a usar o invólucro ao inicializar minha visualização personalizada. Isso, por sua vez, mata meu .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 {
        ...
    }
}

O que parece funcionar é usar em var nspace: Namespace.IDvez de @Namespace var nspacee:

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 {
        ...
    }
}

Isso pode causar problemas em outro lugar? Existe uma maneira melhor?

Respostas

1 Asperi Sep 05 2020 at 01:09

Isso pode causar problemas em outro lugar? Existe uma maneira melhor?

Não é pior / melhor, é a única maneira correta. Vamos ver a API:

O Namespace.IDé o valor usado para identificar o namespace do efeito correspondente

/// 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 !!

e como é visto

    ///   - 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