forked from fermoya/SwiftUIPager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfiniteExampleView.swift
104 lines (94 loc) · 3.69 KB
/
InfiniteExampleView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
// InfiniteExampleView.swift
// SwiftUIPagerExample
//
// Created by Fernando Moya de Rivas on 02/03/2020.
// Copyright © 2020 Fernando Moya de Rivas. All rights reserved.
//
import SwiftUI
struct InfiniteExampleView: View {
@StateObject var page1 = Page.withIndex(2)
@StateObject var page2 = Page.first()
@State var data1 = Array(0..<7)
@State var isPresented: Bool = false
var data2 = Array(0..<20)
var body: some View {
NavigationView {
GeometryReader { proxy in
VStack(spacing: 10) {
Text("Appending on the fly")
.bold()
.padding(.top)
Pager(page: page1,
data: data1,
id: \.self) {
self.pageView($0)
}
.singlePagination(ratio: 0.5, sensitivity: .high)
.onPageWillChange({ (page) in
print("Page will change to: \(page)")
})
.onPageChanged({ page in
print("Page changed to: \(page)")
if page == 1 {
let newData = (1...5).map { data1.first! - $0 }.reversed()
withAnimation {
page1.index += newData.count
data1.insert(contentsOf: newData, at: 0)
isPresented.toggle()
}
} else if page == self.data1.count - 2 {
guard let last = self.data1.last else { return }
let newData = (1...5).map { last + $0 }
withAnimation {
isPresented.toggle()
data1.append(contentsOf: newData)
}
}
})
.pagingPriority(.simultaneous)
.preferredItemSize(CGSize(width: 200, height: 100))
.itemSpacing(10)
.background(Color.gray.opacity(0.2))
.alert(isPresented: self.$isPresented, content: {
Alert(title: Text("Congratulations!"),
message: Text("Five more elements were appended to your Pager"),
dismissButton: .default(Text("Okay!")))
})
Spacer()
Text("Looping Pager")
.bold()
Pager(page: self.page2,
data: self.data2,
id: \.self) {
self.pageView($0)
}
.pagingPriority(.simultaneous)
.loopPages()
.sensitivity(.high)
.itemSpacing(10)
.itemAspectRatio(1.3, alignment: .start)
.padding(20)
.background(Color.gray.opacity(0.2))
}
.navigationBarTitle("Infinite Pagers", displayMode: .inline)
}
}.navigationViewStyle(StackNavigationViewStyle())
}
func pageView(_ page: Int) -> some View {
ZStack {
Rectangle()
.fill(Color.yellow)
NavigationLink(destination: Text("Page \(page)")) {
Text("Page \(page)")
}
}
.cornerRadius(5)
.shadow(radius: 5)
}
}
struct InfiniteExampleView_Previews: PreviewProvider {
static var previews: some View {
InfiniteExampleView()
}
}