首页 > 解决方案 > 从 Core Data 加载图像使 NavigationView 过渡变得跳跃

问题描述

在我的 SwiftUI 应用程序中,当用户单击列表中的项目时,它会将用户带到详细信息视图。在详细信息视图中,我们需要显示从 Core Data 加载的图像。图像数据会在 NavigationView 过渡完成之前准备好,但 NavigationView 过渡会在图像出现时变得跳跃。

这是查看跳跃过渡的 GIF 图像:https ://gph.is/g/aQqBqRA

请在下面找到我的代码:

       Image(uiImage: self.currentImage)
            .resizable()
            .aspectRatio(contentMode: .fit)
        }
        .onAppear {
            DispatchQueue.main.async {
                self.currentImage = UIImage(data: issue.photosArray[0].photoData!)!
            }
        }

如何修复跳跃的过渡?

标签: core-dataswiftuitransition

解决方案


在动画开始之前.onAppear调用,因此您加载的图像会在动画期间导致视图重建,从而导致观察效果。

如果图像在动画之前无法完全加载,那么最好推迟到动画完成,比如

   Image(uiImage: self.currentImage)
        .resizable()
        .aspectRatio(contentMode: .fit)
    }
    .onAppear {
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
            self.currentImage = UIImage(data: issue.photosArray[0].photoData!)!
        }
    }

推荐阅读