ios - matchedGeometryEffect 动画不流畅
问题描述
我正在尝试做一个简单matchedGeometryEffect
的事情,但它不起作用。我有一个简单Image
的框架,它有一个固定的框架,我想将它设置为一个Image
占据整个屏幕的动画。这就是它现在的样子,正如你所看到的,它并没有顺利过渡:
这是我的代码:
struct ContentView: View {
@State private var enlarged = false
@Namespace private var animation
var body: some View {
ZStack {
if (enlarged) {
Image("dunes")
.resizable()
.aspectRatio(contentMode: .fill)
.matchedGeometryEffect(id: "image", in: animation)
.ignoresSafeArea(.all, edges: .all)
} else {
Image("dunes")
.resizable()
.aspectRatio(contentMode: .fill)
.matchedGeometryEffect(id: "image", in: animation)
.frame(width: 300, height: 225)
}
}
.onTapGesture {
withAnimation {
enlarged.toggle()
}
}
}
}
解决方案
为了使matchedGeometryEffect
动画顺利进行,您几乎没有遵循的规则。您必须在matchedGeometryEffect
. 动画必须放在每个图像的末尾,而不是放在.tapGesture
.
以下是修改后的代码:
struct ContentView: View {
@State private var enlarged = false
@Namespace private var namespace
var body: some View {
ZStack {
if enlarged {
Image("dunes")
.resizable()
.matchedGeometryEffect(id: "image", in: namespace)
.aspectRatio(contentMode: .fill)
.ignoresSafeArea(.all, edges: .all)
.animation(.easeInOut)
}
else {
Image("dunes")
.resizable()
.matchedGeometryEffect(id: "image", in: namespace)
.aspectRatio(contentMode: .fill)
.frame(width: 300, height: 225)
.animation(.easeInOut)
}
}
.onTapGesture { enlarged.toggle() }
}
}
推荐阅读
- rust - 用特征别名替换特征绑定表示“在编译时无法知道值的大小”
- android - 使用 C++ 锁定 Android 设备
- angular - 如何使用 Angular 6 在单个组件中渲染多个 ag-grid
- python - 在 sqlite3 中选择小于/高于
- python-3.x - 无法找到正确的元素文本
- asp.net-mvc - 如何从 WebAPI 控制器中使用程序集
- mongodb - 查询和排序集合 MongoDB Stitch
- c - 将双精度传递给 void * 参数的方法
- sql - 如何在postgres中删除特定模式中的每个表?
- android - 空对象引用上的 android.text.Editable android.widget.EditText.getText() 问题