I've used the GeometryReader to give an animation effect on the rocket object, but the background keeps getting affected by it somehow. Also I think that's what's causing the rocket to lag while moving.
- How do I make the rocket go out of the screen in trailing and reappear from leading on a loop?
- How do I stop the background from being affected by the animation?
Below is the code:
import SwiftUI
struct ActionView: View {
@State var animate: Bool = false
let animation: Animation = Animation.linear(duration: 10.0).repeatForever(autoreverses: false)
var body: some View {
GeometryReader { geo in
ZStack {
Image("galaxy")
.resizable()
.edgesIgnoringSafeArea(.all)
.scaledToFill()
.frame(width: geo.size.width, height: geo.size.height)
VStack {
HStack(spacing: 180) {
Image("rocket")
.resizable()
.scaledToFit()
.offset(y: -150)
Image("rocket")
.resizable()
.scaledToFit()
.offset(y: -150)
.frame(width: geo.size.width, height: 120, alignment: .leading)
}
.frame(width: geo.size.width, height: 120, alignment: animate ? .leading : .trailing)
VStack {
Image("me")
.resizable()
.scaledToFit()
.frame(width: 150, height: 200)
.offset(x: 35)
HStack {
Spacer()
Image("DotButton")
.shadow(color: .white, radius: 10, x: 0.0, y: 0.0); Spacer()
Image("SlashButton")
.shadow(color: .white, radius: 10, x: 0.0, y: 0.0); Spacer()
}
}
}
.ignoresSafeArea()
.onAppear {
withAnimation(animation) {
animate.toggle()
}
}
}
}
}
}