gpt4 book ai didi

ios - matchedGeometryEffect 动画效果不流畅

转载 作者:行者123 更新时间:2023-12-05 09:33:22 26 4
gpt4 key购买 nike

我正在尝试做一个简单的 matchedGeometryEffect 但它不起作用。我有一个简单的 Image,它有一个固定的框架,我想将它动画化为一个占据整个屏幕的 Image。这就是它现在的样子,如您所见,它没有顺利过渡:

enter image description here

这是我的代码:

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

关于ios - matchedGeometryEffect 动画效果不流畅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67354343/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com