使用@Namespace进行.matchedGeometryEffect的自定义初始化

时间:2020-09-04 17:46:47

标签: swift xcode swiftui xcode12

我正在尝试将具有@Namespace的{​​{1}}属性的自定义View结构传递给父View。

由于父母将提供.matchedGeometryEffect,所以我使用的是自定义Namespace

当我使用类似于init自定义初始化的语法时,Xcode会在初始化自定义View时强制我使用包装器。反过来会杀死我的@Binding

.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

这会在其他地方引起麻烦吗?有更好的方法吗?

1 个答案:

答案 0 :(得分:1)

这会在其他地方引起麻烦吗?有更好的方法吗?

这并不坏/更好,这是唯一正确的方法。让我们看一下API:

Sub CopyPaste() Dim lastrow As Integer, erow As Long, sheet1 As Worksheet, sheet2 As Worksheet Set sheet1 = Worksheets("Sheet1") Set sheet2 = Worksheets("Sheet2") lastrow = sheet1.Cells(Rows.Count, 1).End(xlUp).Row For i = 2 To lastrow erow = sheet2.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row sheet2.Cells(erow, 2) = sheet1.Cells(i, 3) sheet2.Cells(erow, 3) = sheet1.Cells(i, 4) sheet2.Cells(erow, 4) = sheet1.Cells(i, 9) Next i End Sub 是用于标识匹配效果的名称空间的值

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