动画图像可以在屏幕上连续移动?

发布于 2025-02-08 13:49:37 字数 1300 浏览 1 评论 0原文

关于此 post 我尝试实现无限的图像动画,并且效果很好。但是,一旦我将图像更改为可回理scaledtofill以覆盖整个屏幕作为背景,一个图像比另一个屏幕更高,并且在此期间看起来很恰当地切割动画片。我相信答案很明显,但仍然找不到。

这是一个代码示例/示例:

 struct ImageBackgroundView {
    @State var animate: Bool = false
    let animation: Animation = Animation.linear(duration: 10.0).repeatForever(autoreverses: false)
    
var body: some View {
    GeometryReader { geo in
        HStack(spacing: -1) {
            Image("lightBackground")
                .resizable()
                .scaledToFill()
                
            
            Image("lightBackground")
                .resizable()
                .scaledToFill()
                .frame(width: geo.size.width, alignment: .leading)
        }
        .frame(width: geo.size.width, height: geo.size.height, alignment: animate ? .trailing : .leading)
    }
    .ignoresSafeArea()
    .onAppear {
        withAnimation(animation) {
            animate.toggle()
        }
    }
}

}

正如我之前所说的,它可以与.aspectratio(contentMode:.fit)```在链接线程中提出''一起工作。我相信我无法解决的几何学上存在问题。 感谢您提前帮助!

Regarding the answer in this post I've tried to implement the infinite image animation and it worked just fine. But as soon as I change an image to be resizable and scaledtoFill to cover the whole screen as a background, one image becomes taller than another and it looks quite awfully cut during the animation. I believe the answer is obvious but still can't find it.

Here's a code sample/example:

 struct ImageBackgroundView {
    @State var animate: Bool = false
    let animation: Animation = Animation.linear(duration: 10.0).repeatForever(autoreverses: false)
    
var body: some View {
    GeometryReader { geo in
        HStack(spacing: -1) {
            Image("lightBackground")
                .resizable()
                .scaledToFill()
                
            
            Image("lightBackground")
                .resizable()
                .scaledToFill()
                .frame(width: geo.size.width, alignment: .leading)
        }
        .frame(width: geo.size.width, height: geo.size.height, alignment: animate ? .trailing : .leading)
    }
    .ignoresSafeArea()
    .onAppear {
        withAnimation(animation) {
            animate.toggle()
        }
    }
}

}

Right as I said before, It works just fine with .aspectRatio(contentMode: .fit)` which was proposed in a linked thread. I believe there's a problem in a GeometryReader that I cannot solve.
Appreciate your help in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蒲公英的约定 2025-02-15 13:49:37

首先,您需要了解图像如何工作的一些基础知识。

-scaledtofill:通常,这意味着图像将在填充层所需的时间内增长。不保持其原始比率。

- 拟合:调整大小时的图像保持原始比率。图像不会脱离框架。

-fill:这可能是您需要的。填充允许图像扩展以填写整个图层,同时保持其原始比率。

Firstly, you need to understand some basics of how the images work.

-scaledtoFill: In general, this means the image will grow as much as it needs to be to fill out the layer. Does not maintain its original ratio.

-fit: Image keeps its original ratio when resizing. The image will not go out of frame.

-fill: This is probably the one that you need. fill allow the image to expand to fill out the entire layer while maintain its original ratio.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文