首页 > 解决方案 > SwiftUI:在填充的水平图像中制作 y 偏移的永久动画

问题描述

我正在尝试通过它的 y 轴为“行”内的图像设置动画,这样看起来您的视图会慢慢地垂直滚动整个图像。完成后,它会回溯。我希望这是有道理的。我试图在这段代码中做到这一点:

var body: some View {
        ZStack {
            HStack {
                GeometryReader { geometry in
                    WebImage(url: self.url)
                        .renderingMode(.original)
                        .resizable()
                        .placeholder {
                            ImageStore.shared.image(name: "exploras-icon")
                        }
                        .aspectRatio(contentMode: .fill)
                        .animate {
                          // animate by y offset within bounds of HStack
                        }
                }
            }
            .frame(height: 140)
            .clipped()
        }
    }

非常感谢任何帮助/指导!

标签: iosswiftimageanimationswiftui

解决方案


这是一个可能的方法的演示。使用 Xcode 11.4 / iOS 13.4 测试

演示

struct TestAutoScrollImage: View {
    @State private var isActive = false
    var body: some View {
        GeometryReader { gp in
            HStack {
                Image("large_image")
            }
            .frame(width: gp.size.width, height: gp.size.height, alignment: self.isActive ? .bottom : .top)
        }
        .edgesIgnoringSafeArea([.top, .bottom])
        .animation(Animation.linear(duration: 5).repeatForever())
        .onAppear {
            self.isActive = true
        }
    }
}

推荐阅读