SwiftUI:在填充的水平图像中永久制作y偏移的动画

问题描述 投票:0回答:1

我正在尝试通过它的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()
        }
    }

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

ios swift image animation swiftui
1个回答
0
投票

这里是可能方法的演示。经过Xcode 11.4 / iOS 13.4的测试]

demo

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
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.