我想用.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()
        }
    }
}

视图会向上滑动,但不会正确设置动画,因为它具有淡入淡出效果.我假设这与应用于每个视图的默认淡入淡出过渡有关,我试图设置一个自定义淡入淡出过渡,但在这种情况下找不到合适的淡入淡出淡出过渡.

enter image description here

推荐答案

修饰语的位置很重要,对于matchedGeometryEffect来说更重要.

这是一个固定的变体.Xcode 13.2/iOS 15.2

demo

var body: some View {
    if isShowingDetail {
        VStack {
            Rectangle()
                .foregroundColor(.black)
                .aspectRatio(contentMode: .fit)
                .edgesIgnoringSafeArea(.top)
                                            // !! applied here !!
                .matchedGeometryEffect(id: "image", in: namespace, properties: .position)
            Spacer()
        }
        .onTapGesture {
            changeView()
        }
    } else {
        Rectangle()
            .foregroundColor(.black)
            .aspectRatio(contentMode: .fit)
            .cornerRadius(8)
                               // !! applied here !!
            .matchedGeometryEffect(id: "image", in: namespace, properties: .position)
            .onTapGesture {
                changeView()
            }
    }
}

Ios相关问答推荐

SWIFT图表轴订购问题

删除领域中的cartItem时出现问题

执行devicectl:ProcessException时出错:进程异常退出:错误:命令超时超过5.0秒

分配给指针会通过 class_addMethod 导致 EXC_BAD_ACCESS

清除/删除 NavigationController 中的多个 ViewController

在 iOS 上以后台模式安排任务(屏幕时间 API)

如何制作支持不同语言的iOS应用

如何在Swift/Combine中从timer.publish属性传递两个参数到任意方法

iOS上从谷歌云存储API不断收到网络连接已丢失的错误信息

部分保留 UITextField 中的占位符

在 SwiftUI 中使用 .rotation3DEffect 在视图之间翻转

如何在swiftui中更改可搜索的搜索图标 colored颜色

转换在 NavigationView 中不起作用

以正确的方式在 Text() 中使用 colored颜色

在 UICollectionView 上从右到左对齐

UILabel - 字符串作为文本和链接

'+entityForName: nil 不是合法的 NSManagedObjectContext 参数 - 核心数据

如何在上传到服务器之前在 iOS 上压缩/调整图像大小?

Xcode 4 + iOS 4.3:存档类型不存在打包程序

IOS - 从 UITextView 中删除所有填充