使
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() }
}
}