我想用.matchedGeometryEffect
来实现图像点击时的向上滑动动画.我只想用它来设置位置变化的动画,因为两幅图像的帧是相同的.在这个例子中,我用一个黑色的矩形替换图像,因为它也可以复制.
struct ContentView: View {
@Namespace var namespace
@State var isShowingDetail = false
var body: some View {
if isShowingDetail {
VStack {
Rectangle()
.foregroundColor(.black)
.matchedGeometryEffect(id: "image", in: namespace, properties: .position)
.aspectRatio(contentMode: .fit)
.edgesIgnoringSafeArea(.top)
Spacer()
}
.onTapGesture {
changeView()
}
} else {
Rectangle()
.foregroundColor(.black)
.matchedGeometryEffect(id: "image", in: namespace, properties: .position)
.aspectRatio(contentMode: .fit)
.cornerRadius(8)
.onTapGesture {
changeView()
}
}
}
private func changeView() {
withAnimation(.easeOut(duration: 0.3)){
isShowingDetail.toggle()
}
}
}
视图会向上滑动,但不会正确设置动画,因为它具有淡入淡出效果.我假设这与应用于每个视图的默认淡入淡出过渡有关,我试图设置一个自定义淡入淡出过渡,但在这种情况下找不到合适的淡入淡出淡出过渡.