Onboarding screens swiftui

 

//

//  OnboardingSwiftUiApp.swift

//  OnboardingSwiftUi

//

//  Created by Tushar Ahmed on 23/6/25.

//


import SwiftUI


@main

struct OnboardingSwiftUiApp: App {

    var body: some Scene {

        WindowGroup {

            ContentView()

        }

    }

}


//

//  OnboardingItem.swift

//  OnboardingSwiftUi

//

//  Created by Tushar Ahmed on 23/6/25.

//


import Foundation


struct OnboardingItem: Identifiable {

    let id = UUID()

    let image: String

    let title: String

    let description: String

}



let onboardingItems = [

    OnboardingItem(image: "fireworks_img", title: "Celebrate", description: "Experience magical moments and spark joy in every interaction."),

    OnboardingItem(image: "leaf_img", title: "Grow", description: "Nurture your potential and flourish with natural progress."),

    OnboardingItem(image: "potion_img", title: "Transform", description: "Unlock powerful features that change everything.")

]





//

//  ContentView.swift

//  OnboardingSwiftUi

//

//  Created by Tushar Ahmed on 23/6/25.

//


import SwiftUI


struct ContentView: View {

    @State private var index = 0

    

    func getHeadingText(for pageIndex: Int) -> String {

        switch pageIndex {

        case 0:

            return "Welcome"

        case 1:

            return "Discover"

        case 2:

            return "Explore"

        default:

            return "Welcome"

        }

    }

    

    func textGradientColors(for pageIndex: Int) -> [Color] {

        switch pageIndex {

        case 0:

            return [Color.orange, Color.red, Color.pink]

        case 1:

            return [Color.cyan, Color.teal, Color.blue]

        case 2:

            return [Color.purple, Color.indigo, Color.pink]

        default:

            return [Color.teal, Color.blue, Color.purple]

        }

    }

    

    func gradientColors(for pageIndex: Int) -> [Color] {

        switch pageIndex {

        case 0:

            return [

                Color(red: 1.0, green: 0.4, blue: 0.2),

                Color(red: 1.0, green: 0.6, blue: 0.0),

                Color(red: 1.0, green: 0.2, blue: 0.4),

                Color(red: 0.9, green: 0.5, blue: 0.1),

                Color(red: 1.0, green: 0.7, blue: 0.3),

                Color(red: 0.9, green: 0.3, blue: 0.5),

                Color(red: 0.8, green: 0.6, blue: 0.2),

                Color(red: 1.0, green: 0.5, blue: 0.2),

                Color(red: 0.8, green: 0.4, blue: 0.6)

            ]

        case 1:

            return [

                Color(red: 0.0, green: 0.7, blue: 0.9),

                Color(red: 0.2, green: 0.8, blue: 0.6),

                Color(red: 0.0, green: 0.6, blue: 0.8),

                Color(red: 0.3, green: 0.8, blue: 0.5),

                Color(red: 0.1, green: 0.9, blue: 0.7),

                Color(red: 0.0, green: 0.7, blue: 0.6),

                Color(red: 0.4, green: 0.7, blue: 0.3),

                Color(red: 0.2, green: 0.8, blue: 0.4),

                Color(red: 0.1, green: 0.6, blue: 0.5)

            ]

        case 2:

            return [

                Color(red: 0.4, green: 0.2, blue: 0.8),

                Color(red: 0.6, green: 0.3, blue: 0.9),

                Color(red: 0.5, green: 0.1, blue: 0.7),

                Color(red: 0.7, green: 0.4, blue: 0.8),

                Color(red: 0.5, green: 0.2, blue: 0.9),

                Color(red: 0.4, green: 0.0, blue: 0.6),

                Color(red: 0.6, green: 0.5, blue: 0.7),

                Color(red: 0.8, green: 0.3, blue: 0.6),

                Color(red: 0.3, green: 0.1, blue: 0.5)

            ]

        default:

            return [

                Color(red: 0.2, green: 0.4, blue: 0.8),

                Color(red: 0.4, green: 0.2, blue: 0.9),

                Color(red: 0.6, green: 0.1, blue: 0.7),

                Color(red: 0.3, green: 0.5, blue: 0.8),

                Color(red: 0.5, green: 0.3, blue: 0.9),

                Color(red: 0.4, green: 0.2, blue: 0.6),

                Color(red: 0.2, green: 0.6, blue: 0.7),

                Color(red: 0.6, green: 0.4, blue: 0.8),

                Color(red: 0.3, green: 0.2, blue: 0.5)

            ]

        }

    }


    var body: some View {

        ZStack {

            MeshGradient(

                width: 3,

                height: 3,

                points: [

                    .init(0, 0), .init(0.5, 0), .init(1, 0),

                    .init(0, 0.5), .init(0.5, 0.5), .init(1, 0.5),

                    .init(0, 1), .init(0.5, 1), .init(1, 1)

                ],

                colors: gradientColors(for: index)

            )

            .ignoresSafeArea()

            .animation(.easeInOut(duration: 0.8), value: index)

            

            VStack(spacing: 40) {

                Text(getHeadingText(for: index))

                    .font(.body)

                    .fontWeight(.semibold)

                    .padding(6)

                    .background(

                        MeshGradient(

                            width: 2,

                            height: 2,

                            points: [

                                .init(0, 0), .init(1, 0),

                                .init(0, 1), .init(1, 1)

                            ],

                            colors: textGradientColors(for: index)

                        )

                    )

                

                    .foregroundColor(.white)

                    .animation(.easeInOut(duration: 0.6), value: index)

                TabView(selection: $index) {

                    ForEach(Array(onboardingItems.enumerated()), id: \.1.id) { i, item in

                        VStack(spacing: 40) {

                            Image(item.image)

                                .resizable()

                                .scaledToFit()

                                .frame(width: 180, height: 180)

                                .clipShape(RoundedRectangle(cornerRadius: 20))

                            

                            VStack(spacing: 24) {

                                Text(item.title)

                                    .font(.system(size: 32, weight: .bold, design: .rounded))

                                    .foregroundColor(.white)

                                    .tracking(0.5)

                                

                                Text(item.description)

                                    .font(.system(size: 18, weight: .medium, design: .default))

                                    .foregroundColor(.white.opacity(0.70))

                                    .multilineTextAlignment(.center)

                                    .lineSpacing(6)

                                    .frame(maxWidth: 280)

                            }

                        }

                        .tag(i)

                    }

                }

                .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))

                

                // Indicators

                HStack(spacing: 8) {

                    ForEach(0..<onboardingItems.count, id: \.self) { i in

                        Rectangle()

                            .fill(index == i ? Color.orange : Color.white.opacity(0.5))

                            .frame(width: index == i ? 10 : 20, height: 8)

                            .animation(.easeInOut(duration: 0.3), value: index)

                    }

                }

                

                Spacer()

                

                HStack(spacing: 20) {

                    // Previous button

                    Button(action: {

                        withAnimation(.easeInOut(duration: 0.4)) {

                            if index > 0 {

                                index -= 1

                            }

                        }

                    }) {

                        HStack(spacing: 8) {

                            Image(systemName: "chevron.left")

                                .font(.system(size: 14, weight: .medium))

                            Text("Previous")

                                .font(.system(size: 14, weight: .medium))

                        }

                        .foregroundColor(index > 0 ? .white : .white.opacity(0.4))

                        .padding(.horizontal, 20)

                        .padding(.vertical, 12)

                        .background(

                            RoundedRectangle(cornerRadius: 25)

                                .fill(Color.white.opacity(index > 0 ? 0.15 : 0.05))

                                .overlay(

                                    RoundedRectangle(cornerRadius: 25)

                                        .stroke(Color.white.opacity(index > 0 ? 0.3 : 0.1), lineWidth: 1)

                                )

                        )

                    }

                    .disabled(index == 0)

                    

                    Spacer()

                    

                    // Next button

                    Button(action: {

                        withAnimation(.easeInOut(duration: 0.4)) {

                            if index < onboardingItems.count - 1 {

                                index += 1

                            }

                        }

                    }) {

                        HStack(spacing: 8) {

                            Text(index == onboardingItems.count - 1 ? "Get Started" : "Next")

                                .font(.system(size: 14, weight: .semibold))

                            if index < onboardingItems.count - 1 {

                                Image(systemName: "chevron.right")

                                    .font(.system(size: 14, weight: .medium))

                            }

                        }

                        .foregroundColor(.white)

                        .padding(.horizontal, 24)

                        .padding(.vertical, 12)

                        .background(

                            RoundedRectangle(cornerRadius: 25)

                                .fill(

                                    LinearGradient(

                                        gradient: Gradient(colors: [Color.orange, Color.pink]),

                                        startPoint: .leading,

                                        endPoint: .trailing

                                    )

                                )

                            

                        )

                    }

                }

                .padding(.horizontal, 20)

                

            }

            .padding(.horizontal, 24)

            .padding(.vertical, 10)

        }

    }

}


#Preview {

    ContentView()

}

Comments

Popular posts from this blog

Learning basics for Backend Developer | build setup

Bponi November 26 2025

make this project