Init personalizzato con @Namespace per .matchedGeometryEffect
Sto cercando di passare una struttura di visualizzazione personalizzata con una @Namespaceproprietà per a .matchedGeometryEffecta una vista padre.
Poiché il genitore fornirà il, Namespacesto usando un'usanza init.
Quando utilizzo la sintassi analoga all'inizializzazione @Bindingpersonalizzata, Xcode mi obbliga a utilizzare il wrapper durante l'inizializzazione della mia visualizzazione personalizzata. Questo a sua volta uccide il mio .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 {
...
}
}
Quello che sembra funzionare sta usando var nspace: Namespace.IDinvece di @Namespace var nspacee poi:
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 {
...
}
}
Questo può causare problemi da qualche altra parte? Esiste un modo migliore?
Risposte
Questo può causare problemi da qualche altra parte? Esiste un modo migliore?
Non è peggio / meglio è l'unico modo corretto. Vediamo API:
Il Namespace.IDvalore è utilizzato per identificare lo spazio dei nomi dell'effetto corrispondente
/// 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 come si vede
/// - 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