简体   繁体   English

SwiftUI AsyncImage animation 不当行为

[英]SwiftUI AsyncImage animation misbehavior

While testing out SwiftUI, I've found that AsyncImage does not work well while animating a transition.在测试 SwiftUI 时,我发现 AsyncImage 在动画过渡时效果不佳。 It seems to settle on its final position of the transition before the rest of the UI has moved there, making the animation seem weird or off.在 UI 的 rest 移到那里之前,它似乎已确定其最终的过渡 position,这使得 animation 看起来很奇怪或偏离。

My main question is: is there any way to access the SwiftUI animation of the AsyncImage and make it work with other animations elsewhere in the app?我的主要问题是:有什么方法可以访问 AsyncImage 的 SwiftUI animation 并使其与应用程序其他地方的其他动画一起使用?

The weird part is that, if I change it to some other view (that doesn't have animations), the transition behaves correctly, so I believe that the root of the problem is something to do with AsyncImage's default animation for changing between its phases (loading, failed or loaded).奇怪的是,如果我将其更改为其他视图(没有动画),则转换行为正确,因此我认为问题的根源与 AsyncImage 的默认 animation 用于在其phases之间进行更改有关(加载、失败或加载)。

The overlay that is presented is described as such:呈现的叠加层是这样描述的:

    if isBottomSheetVisible {
                VStack(alignment: .leading) {
                    AccountSelectorHeader()
                    ForEach(accounts) { account in
                        AccountRow(
                            account: account,
                            isLast: accounts.last == account
                        )
                    }
                }
                .padding(.bottom, 24)
                .background(Color(.tableViewHeaderBackgroundColor)
                                .cornerRadius(24, corners: [.topLeft, .topRight])
                                .edgesIgnoringSafeArea(.bottom)
                )
                .transition(
                    .move(edge: .bottom)
                )
            }

And each image is only a standard AsyncImage inside the AccountRow view:每个图像只是 AccountRow 视图中的一个标准 AsyncImage:

    AsyncImage(url: URL(string: account.image)) {
            $0
                .resizable()
                .clipShape(Circle())
        } placeholder: {
            ProgressView()
        }

I faced a similar issue and it worked fairly well if you use the init(url:scale:transaction:content:) .我遇到了类似的问题,如果您使用init(url:scale:transaction:content:) ,它工作得相当好。

With the code below I got the image move in with the sheet and on disappear it didn't move but was removed as the sheet obscured it:使用下面的代码,我将图像与纸张一起移动,并且在消失时它没有移动,但由于纸张遮住了它而被移除:

AsyncImage(
            url: URL(string: url),
            transaction: Transaction(animation: .default)
        ) { phase in
            switch phase {
            case .success(let image):
                image
                    .resizable()
            default:
                Image("defaultImage")
                    .resizable()
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM