动画图像可以在屏幕上连续移动?
关于此 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您需要了解图像如何工作的一些基础知识。
-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.