0

我有一个动画背景图像在屏幕上水平移动。我导航以进行应用内购买,无论是在输入密码之前购买还是取消,当我返回时背景图像不再是动画。如果我在进入要求输入密码的框之前取消,则图像仍在动画中。我怎样才能防止这种情况发生,或者重新启动动画?

struct Home: View {
    @State private var animateBackground = false
    var body: some View {
        ZStack {

            ImageBackgroundView(gameSettings: self.gameSettings, animate: self.animateBackground)
            
            //more code...
        }
    }
}

struct ImageBackgroundView: View {
    
    @ObservedObject var gameSettings: GameSettings
    @State var animate: Bool
    
    let animation: Animation = Animation.linear(duration: 100.0).repeatForever(autoreverses: false)
    
    var body: some View {
        GeometryReader { geo in
            HStack(spacing: -1) {
                Image("\(AppConstants.levels[gameSettings.gameLevel].background)")
                    .resizable()
                    .scaledToFill()
                    .frame(height: geo.size.height)

                Image("\(AppConstants.levels[gameSettings.gameLevel].background)")
                    .resizable()
                    .scaledToFill()
                    .frame(width: geo.size.width, height: geo.size.height, alignment: .leading)
            }
            .frame(width: geo.size.width, height: geo.size.height,
                   alignment: animate ? .trailing : .leading)
        }
        .ignoresSafeArea()
        .onAppear {
            withAnimation(animation) {
                animate.toggle()
            }
        }
    }
}

如果我在这里取消,动画仍然有效:

在此处输入图像描述

如果我在此处取消或完成购买,则不再有动画:

在此处输入图像描述

4

0 回答 0