-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContentView3_iOS14.swift
115 lines (99 loc) · 3.56 KB
/
ContentView3_iOS14.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
105
106
107
108
109
110
111
112
113
114
115
//
// ContentView3_iOS14.swift
// swiftUI_scrollView
//
// Created by Sasha Myshkina on 14/08/2023.
//
import SwiftUI
// This view showcases vertical scroll with
// ScrollViewReader to scroll programatically to the top, to the bottom, and to a single view inside the ScrollView content
struct ContentView3_iOS14: View {
// MARK: - Declarations
enum Constant {
static let smallFrame = CGSize(width: 300, height: 100)
static let cornerRadius = 25.0
static let safeAreaPadding = 40.0
static let vstackSpacing = 20.0
static let buttonViewHeight = 100.0
}
// MARK: - Properties
@StateObject var viewModel = ScrollViewContentModel()
@State var currentId: Int = 0
// MARK: - Body
var body: some View {
VStack(spacing: Constant.vstackSpacing) {
ScrollViewReader { proxy in
buttonView(with: proxy)
ScrollView(.vertical) {
LazyVStack {
ForEach(viewModel.contentItems) { item in
rectangleView(colour: item.colour, text: "\(item.id)")
.containerRelativeFrame([.vertical, .horizontal])
.id(item.id)
}
}
}
// as this example targets only auto scrolling,
// manual scrolling is disable. To observe manual scrolling positions,
// use ScrollView + PreferenceKey
.scrollDisabled(true)
.safeAreaPadding(.horizontal, Constant.safeAreaPadding)
}
}
.safeAreaPadding(.bottom, Constant.safeAreaPadding)
}
private func rectangleView(colour: Color, text: String) -> some View {
RoundedRectangle(cornerRadius: Constant.cornerRadius)
.fill(colour)
.overlay {
Text(text)
}
}
private func buttonView(with proxy: ScrollViewProxy) -> some View {
ZStack {
Color.black
HStack {
topButton(with: proxy)
VStack {
Button("Next page") {
if currentId < viewModel.contentItems.count - 1 {
proxy.scrollTo(currentId + 1)
currentId = currentId + 1
}
}
.padding(5)
Text("Current page: \(currentId)")
.foregroundColor(.white)
}
bottomButton(with: proxy)
}
}
.frame(height: Constant.buttonViewHeight)
}
private func topButton(with proxy: ScrollViewProxy) -> some View {
Button(action: {
proxy.scrollTo(0)
currentId = 0
}, label: {
Image(systemName: "arrow.up")
.foregroundColor(.white)
})
.padding(.horizontal, Constant.safeAreaPadding)
}
private func bottomButton(with proxy: ScrollViewProxy) -> some View {
Button(action: {
let lastItemId = viewModel.contentItems.count - 1
proxy.scrollTo(lastItemId)
currentId = lastItemId
}, label: {
Image(systemName: "arrow.down")
.foregroundColor(.white)
})
.padding(.horizontal, Constant.safeAreaPadding)
}
}
struct ContentView3_iOS14_Previews: PreviewProvider {
static var previews: some View {
ContentView3_iOS14()
}
}