SWIFTUI Image Slideshow using TabView not working

Hi!

The code for the Image Slideshow/Image Gallery is not working. Here's the code. I'll have the questions below.

Import SwiftUI



struct Tab1: View {

    private var numberOfImages = 3

    private let timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect()

    @State private var currentIndex = 0

    var body: some View {
        GeometryReader { proxy in
            TabView(selection:$currentIndex) {
            ForEach(0..<numberOfImages) { num in

                Image("\(num)")
                    .resizable()
                    .scaledToFit()
                    .tag("num")
            }
          }
              .tabViewStyle(PageTabViewStyle(indexDisplayMode: .always))
           
.frame(width: proxy.size.width, height: proxy.size.height / 3).edgesIgnoringSafeArea(.top)

            .onReceive(timer, perform: { _ in
                withAnimation {
                currentIndex = currentIndex < numberOfImages ? currentIndex + 1 : 0
                }
            })
        }
        }
    }

struct Tab1_Previews: PreviewProvider {

    static var previews: some View {

        Tab1()

    }

}

As you can see it's not much but I still get a lot of problems with it.

  1. The slider in the simulation doesn't ignore the safe area as it says, however in the live preview the slider is ignoring the safe area.

  2. I am able to drag the image up and down?? and not only vertically like I want

  3. The slider is on a timer and switch every 10 seconds, now this brings some issues.

3.1 The timer doesn't reset after manually interacting with it (through scrolling)

3.2 Once the slider is on the last image it resets itself going back to image one, but I can't do this manually.

What have I done wrong and how can it be fixed?

Thanks for every kind of help, much appreciated. I´ve been stuck on this for months trying to figure it out myself.

Answered by Gharfield in 694381022

Solved

Accepted Answer

Solved

SWIFTUI Image Slideshow using TabView not working
 
 
Q