0

I have created a view pager and for showing a simple image banner but the issue is the animation is not working properly. the issue is the transition b/w image 0 to image 1 is very fast without a proper animation ans same goes for image last image to first image.

but for all the image in between the animation is working properly how to fix this ??

import SwiftUI
import Kingfisher

struct HorizontalPagerView: View {
    let servicesForBanner: [ServicesItem]
    @State private var selectedPage = 0
    @State private var timer: Timer?
    @State private var isUserInteracting = false
    @State private var timeDelay = 5.0
    let onClick: (ServicesItem) -> Void

    var body: some View {
        VStack(spacing: 10) {
            TabView(selection: $selectedPage) {
                ForEach(0..<servicesForBanner.count, id: \.self) { index in
                    RoundedRectangle(cornerRadius: 30)
                        .fill(Color.white)
                        .padding(.horizontal, 20)
                        .frame(height: 200)
                        .overlay(
                            BannerImageView(imageUrl: servicesForBanner[index].serviceImageUrl)
                        )
                        .tag(index)
                        .onTapGesture {
                            onClick(servicesForBanner[index])
                        }
                }
            }
            .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
            .frame(height: 200)
            .gesture(
                DragGesture()
                    .onChanged { _ in
                        if !isUserInteracting {
                            isUserInteracting = true
                            stopAutoScroll()
                        }
                    }
                    .onEnded { _ in
                        isUserInteracting = false
                        resetAutoScrollTimer()
                    }
            )
            .onAppear {
                startAutoScroll()
            }
            .onDisappear {
                stopAutoScroll()
            }
            
            // Dots indicator for current page
            HStack(spacing: 8) {
                ForEach(0..<servicesForBanner.count, id: \.self) { index in
                    Circle()
                        .fill(index == selectedPage ? Color.primary : Color.gray.opacity(0.4))
                        .frame(width: 8, height: 8)
                }
            }
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color.white)
        .padding(.vertical)
    }

    private func startAutoScroll() {
        stopAutoScroll()
        timer = Timer.scheduledTimer(withTimeInterval: timeDelay, repeats: true) { _ in
            guard !isUserInteracting else { return }
            
            withAnimation(.smooth(duration: 1.0)) {
                selectedPage = (selectedPage + 1) % servicesForBanner.count
                if(selectedPage != 1){
                    resetAutoScrollTimer()
                }
            }
        }
    }

    private func stopAutoScroll() {
        timer?.invalidate()
        timer = nil
    }

    private func resetAutoScrollTimer() {
        stopAutoScroll()
        startAutoScroll()
    }
}

struct BannerImageView: View {
    let imageUrl: String

    var body: some View {
        KFImage(URL(string: NetworkConfig.basePath + imageUrl))
            .placeholder {
                Color.gray.opacity(0.5)
                    .frame(maxWidth: .infinity)
                    .frame(height: 200)
                    .clipShape(RoundedRectangle(cornerRadius: 10))
            }
            .resizable()
            .scaledToFit()
            .frame(maxWidth: .infinity)
            .frame(height: 200)
            .clipShape(RoundedRectangle(cornerRadius: 10))
    }
}
1

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.