Question: Quick Xcode Question: I have made a loading screen for my app. The goal is that it appears for 1 0 secs before going to

Quick Xcode Question:
I have made a loading screen for my app. The goal is that it appears for 10 secs before going to the contentView, in addition the in the loading text to have an animation for three periods to have a different display configuration every seconds. How do i achieve this? Code Below: import SwiftUIimport AVFoundationstruct LoadingView: View { @State private var isLoading = true @State private var loadingText = "Loading..." @State private var colors: [Color]=[] @State private var timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect() @State private var currentIndex =0 @State private var showDots = true // State to control visibility of dots @State private var audioPlayer: AVAudioPlayer? var body: some View { ZStack { LinearGradient(gradient: Gradient(colors: colors), startPoint: .topLeading, endPoint: .bottomTrailing).animation(.easeInOut(duration: 2)).edgesIgnoringSafeArea(.all) VStack { Spacer() Spacer()// Spacer to push text to the bottom third Text(loadingText).font(.system(size: 30))// Cut the size in half .foregroundColor(.black)// Make it black .opacity(loadingText.isEmpty ?0 : 1).animation(Animation.easeInOut(duration: 0.5).repeatForever()) Spacer()}}.onReceive(timer){_ in if currentIndex < loadingText.count {// Check if character is a dot and toggle visibility if loadingText[loadingText.index(loadingText.startIndex, offsetBy: currentIndex)]=="."{ showDots.toggle()} currentIndex +=1} else { currentIndex =0 loadingText = "Loading..." }}.onAppear { setupColors() playSound() DispatchQueue.main.asyncAfter(deadline: .now()+3){ isLoading = false }}.fullScreenCover(isPresented: $isLoading, content: ContentView.init)} private func setupColors(){ var randomColors: [Color]=[] for _ in 0..<8{ let red = Double.random(in: 0...1) let green = Double.random(in: 0...1) let blue = Double.random(in: 0...1) randomColors.append(Color(red: red, green: green, blue: blue))} colors = randomColors } private func playSound(){ if let soundFilePath = Bundle.main.path(forResource: "loadingSound", ofType: "mp3"){ let soundFileURL = URL(fileURLWithPath: soundFilePath) do { audioPlayer = try AVAudioPlayer(contentsOf: soundFileURL) audioPlayer?.prepareToPlay() audioPlayer?.numberOfLoops =-1 audioPlayer?.play()} catch { print("Error loading sound file: \(error.localizedDescription)")}} else { print("Sound file not found")}}}struct LoadingView_Previews: PreviewProvider { static var previews: some View { LoadingView()}}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!