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 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.publishevery: on: main, in: commonautoconnect @State private var currentIndex @State private var showDots true State to control visibility of dots @State private var audioPlayer: AVAudioPlayer? var body: some View ZStack LinearGradientgradient: Gradientcolors: colors startPoint: topLeading, endPoint: bottomTrailinganimationeaseInOutduration: edgesIgnoringSafeAreaall VStack Spacer Spacer Spacer to push text to the bottom third TextloadingTextfontsystemsize: Cut the size in half foregroundColorblack Make it black opacityloadingTextisEmpty : animationAnimationeaseInOutduration: repeatForever SpaceronReceivetimer in if currentIndex loadingText.count Check if character is a dot and toggle visibility if loadingTextloadingTextindexloadingTextstartIndex, offsetBy: currentIndex showDots.toggle currentIndex else currentIndex loadingText "Loading..." onAppear setupColors playSound DispatchQueue.main.asyncAfterdeadline: now isLoading false fullScreenCoverisPresented: $isLoading, content: ContentView.init private func setupColors var randomColors: Color for in let red Double.randomin: let green Double.randomin: let blue Double.randomin: randomColors.appendColorred: red, green: green, blue: blue colors randomColors private func playSound if let soundFilePath Bundle.main.pathforResource: "loadingSound", ofType: mp let soundFileURL URLfileURLWithPath: soundFilePath do audioPlayer try AVAudioPlayercontentsOf: soundFileURL audioPlayer?.prepareToPlay audioPlayer?.numberOfLoops audioPlayer?.play catch printError loading sound file: errorlocalizedDescription else printSound file not found"struct LoadingViewPreviews: 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
