swift - Custom init with @Namespace for .matchedGeometryEffect
问题描述
I'm trying to pass a custom View struct with a @Namespace
property for a .matchedGeometryEffect
to a parent View.
Since the parent will be providing the Namespace
I am using a custom init
.
When I use the syntax analogous to the @Binding
custom initialization Xcode forces me to use the wrapper when initializing my custom View. That in turn kills my .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 {
...
}
}
What seems to work is using var nspace: Namespace.ID
instead of @Namespace var nspace
and then:
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 {
...
}
}
Can this cause trouble somewhere else? Is there a better way?
解决方案
Can this cause trouble somewhere else? Is there a better way?
It is not worse/better it is the only correct way. Let see API:
The Namespace.ID
is value used to identify namespace of matched effect
/// 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 !!
and as it is seen
/// - 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
推荐阅读
- android - 片段中的多个recyclerview
- r - 数据框中的 R grepl
- apache-spark - PySpark 未从 Windows 命令提示符启动
- r - 如何标记以合并数据框
- html - 当一个按钮被按下时,一个 div 字段是可见的
- angular - 在 Angular 中测试具有 HttpClient 依赖关系的抽象类
- swift - UIViewPropertyAnimator 没有在视图之间使用平移手势正确停止
- android - build.gradle 设置如何与“gcloud firebase test android run”参数相关?
- angular - 我在将 Angular 应用程序部署到 github 页面时遇到问题
- c# - HttpClient.GetStringAsync 正在返回“结果”对象?