看完SwiftUI的新增功能后,引起我注意的一个功能是matchGeometryEffect。 这是一个新的SwiftUI效果,可以在两个视图之间插入位置和大小。 让我们看看我们能用它做什么。
基础知识
matchedGeometryEffect
使用您提供的标识符和名称空间定义一组具有同步几何的视图。
func matchedGeometryEffect<ID>(id: ID, in namespace: Namespace.ID, properties: MatchedGeometryProperties= .frame, anchor: UnitPoint = .center, isSource: Bool = true) -> some View where ID : Hashable
使用教程
SwiftUI已经为我们内插视图的属性,而无需matchGeometryEffect。 在下面的示例中,我们可以使用轻击手势为矩形尺寸设置动画。
struct ContentView: View {
@State private var isExpanded = false
var body: some View {
RoundedRectangle(cornerRadius: 10)
.foregroundColor(Color.pink)