Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import SwiftUI
- struct TabItem: Identifiable, Hashable
- {
- let id = UUID()
- let position: Int
- let color: Color
- }
- struct ContentView: View
- {
- @State private var TabItems: [TabItem] =
- [
- TabItem(position: -1, color: .red),
- TabItem(position: 0, color: .green),
- TabItem(position: 1, color: .blue)
- ]
- @State private var selectedTab: Int = 0
- var body: some View
- {
- NavigationView
- {
- TabView(selection: $selectedTab)
- {
- ForEach(TabItems, id: \.position)
- { tab in
- Rectangle()
- .fill(tab.color)
- .containerRelativeFrame([.horizontal, .vertical])
- .overlay
- {
- VStack
- {
- Text("position \(tab.position)")
- }
- }
- .tag(tab.position)
- }
- }
- .tabViewStyle(.page)
- .onAppear()
- {
- selectedTab = 0
- }
- .onChange(of: selectedTab)
- { _, new in
- if(new == TabItems.last!.position)
- {
- print("swiped to end")
- addTabFuture()
- }
- else if(new == TabItems.first!.position)
- {
- print("swiped to beginning")
- addTabPast()
- }
- }
- .navigationTitle("TabTest")
- }
- }
- func addTabFuture()
- {
- let tab = TabItem(position: TabItems.count, color: Color(red: .random(in: 0...1), green: .random(in: 0...1), blue: .random(in: 0...1)))
- TabItems.append(tab)
- }
- func addTabPast()
- {
- let tab = TabItem(position: TabItems.first!.position - 1, color: Color(red: .random(in: 0...1), green: .random(in: 0...1), blue: .random(in: 0...1)))
- TabItems.insert(tab, at: 0)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement