Hi guys I basically have the issue with my background. I made an animated background look in a different SwiftUI file and I want it to be shown as a background in another view. Can you please help me do that?
import SwiftUI
struct AnimatedBackground: View {
var itemsPerRow = 6
@State var isAnimating = false
var body: some View {
VStack(spacing: 0){
ForEach(0..<getNumberOfRows()){i in
HStack(spacing: 0) {
ForEach(0..<self.itemsPerRow){j in
Image(self.getImage(indexLocation: (i * self.itemsPerRow) + j)).resizable().padding()
.frame(width: UIScreen.main.bounds.width/CGFloat(self.itemsPerRow), height: UIScreen.main.bounds.width/CGFloat(self.itemsPerRow))
.opacity(self.isAnimating ? 1 : 0)
.animation(
Animation
.linear(duration: Double.random(in: 1.0...2.0))
.repeatForever(autoreverses: true)
.delay(Double.random(in: 0...1.5))
)
}
}
}
}.onAppear(){
self.isAnimating = true
}
}
func getImage(indexLocation:Int) -> String{
let totalNumbersOfAssets = 30
print(indexLocation % 30)
return String(indexLocation % 30)
}
func getNumberOfRows() -> Int {
let heightPerItem = UIScreen.main.bounds.width/CGFloat(self.itemsPerRow)
return Int(UIScreen.main.bounds.height/heightPerItem) + 1
}
}
struct AnimatedBackground_Previews: PreviewProvider {
static var previews: some View {
AnimatedBackground()
}
}