Skip to content
This repository was archived by the owner on Sep 12, 2024. It is now read-only.

Commit fc90177

Browse files
committed
Add pager
1 parent 8e0646a commit fc90177

File tree

4 files changed

+100
-4
lines changed

4 files changed

+100
-4
lines changed

Snip.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
791B8D7624E28EF800851E2A /* AppState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 791B8D7524E28EF800851E2A /* AppState.swift */; };
1818
791B8D7824E2C6C200851E2A /* SharePicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 791B8D7724E2C6C200851E2A /* SharePicker.swift */; };
1919
79284D6B2507CCDC00210E61 /* WelcomeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79284D6A2507CCDC00210E61 /* WelcomeView.swift */; };
20+
79284D6D2507DE7F00210E61 /* PagerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79284D6C2507DE7F00210E61 /* PagerView.swift */; };
2021
7936F88F24D450B900F09AE7 /* SnipItemsList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7936F88E24D450B900F09AE7 /* SnipItemsList.swift */; };
2122
7936F89324D6377D00F09AE7 /* List.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7936F89224D6377D00F09AE7 /* List.swift */; };
2223
7936F89624D637D800F09AE7 /* Introspect in Frameworks */ = {isa = PBXBuildFile; productRef = 7936F89524D637D800F09AE7 /* Introspect */; };
@@ -88,6 +89,7 @@
8889
791B8D7524E28EF800851E2A /* AppState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppState.swift; sourceTree = "<group>"; };
8990
791B8D7724E2C6C200851E2A /* SharePicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SharePicker.swift; sourceTree = "<group>"; };
9091
79284D6A2507CCDC00210E61 /* WelcomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WelcomeView.swift; sourceTree = "<group>"; };
92+
79284D6C2507DE7F00210E61 /* PagerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PagerView.swift; sourceTree = "<group>"; };
9193
7936F88E24D450B900F09AE7 /* SnipItemsList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SnipItemsList.swift; sourceTree = "<group>"; };
9294
7936F89224D6377D00F09AE7 /* List.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = List.swift; sourceTree = "<group>"; };
9395
7936F89924D845B600F09AE7 /* SnipItemsListAction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SnipItemsListAction.swift; sourceTree = "<group>"; };
@@ -394,6 +396,7 @@
394396
children = (
395397
79F07A2824D3050500469324 /* ImageButton.swift */,
396398
791AC9C424DAE45D00CAB87C /* DeferView.swift */,
399+
79284D6C2507DE7F00210E61 /* PagerView.swift */,
397400
);
398401
path = Misc;
399402
sourceTree = "<group>";
@@ -527,6 +530,7 @@
527530
7936F89F24DA02DD00F09AE7 /* TextField.swift in Sources */,
528531
79C612B12506278D008672BA /* DeepLinkManager.swift in Sources */,
529532
79C463D024F44233009B2DA3 /* MarkdownHTMLViewer.swift in Sources */,
533+
79284D6D2507DE7F00210E61 /* PagerView.swift in Sources */,
530534
79956A4724E57AC700B823E1 /* Dictionary.swift in Sources */,
531535
79BC1D2224D16E67008FD16E /* SnipViewApp.swift in Sources */,
532536
791B8D7624E28EF800851E2A /* AppState.swift in Sources */,
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// PagerView.swift
3+
// Snip
4+
//
5+
// Created by Anthony Fernandez on 9/8/20.
6+
// Copyright © 2020 pictarine. All rights reserved.
7+
//
8+
9+
import SwiftUI
10+
11+
struct PagerView<Content: View>: View {
12+
let pageCount: Int
13+
@Binding var currentIndex: Int
14+
let content: Content
15+
16+
@GestureState private var translation: CGFloat = 0
17+
18+
init(pageCount: Int, currentIndex: Binding<Int>, @ViewBuilder content: () -> Content) {
19+
self.pageCount = pageCount
20+
self._currentIndex = currentIndex
21+
self.content = content()
22+
}
23+
24+
var body: some View {
25+
GeometryReader { geometry in
26+
ZStack {
27+
HStack(spacing: 0) {
28+
self.content.frame(width: geometry.size.width)
29+
}
30+
.frame(width: geometry.size.width, alignment: .leading)
31+
.offset(x: -CGFloat(self.currentIndex) * geometry.size.width)
32+
.offset(x: self.translation)
33+
.animation(.interactiveSpring())
34+
.gesture(
35+
DragGesture().updating(self.$translation) { value, state, _ in
36+
state = value.translation.width
37+
}.onEnded { value in
38+
let offset = value.translation.width / geometry.size.width
39+
let newIndex = (CGFloat(self.currentIndex) - offset).rounded()
40+
self.currentIndex = min(max(Int(newIndex), 0), self.pageCount - 1)
41+
}
42+
)
43+
44+
VStack {
45+
Spacer()
46+
47+
HStack {
48+
ForEach(0..<self.pageCount, id: \.self) { index in
49+
Circle()
50+
.fill(index == self.currentIndex ? Color.white : Color.gray)
51+
.frame(width: 10, height: 10)
52+
}
53+
}
54+
}
55+
.offset(y: 16)
56+
}
57+
}
58+
}
59+
}

Snip/Components/Welcome/WelcomeView.swift

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import SwiftUI
1111
struct WelcomeView: View {
1212

1313
@ObservedObject var viewModel: WelcomeViewModel
14+
@State var currentPage = 0
1415

1516
var body: some View {
1617
ZStack {
@@ -20,20 +21,52 @@ struct WelcomeView: View {
2021
.transition(AnyTransition.opacity)
2122

2223
VStack(alignment: .leading) {
23-
Text("zeijr")
24+
PagerView(pageCount: 3, currentIndex: $currentPage) {
25+
firstView
26+
secondView
27+
}
2428
}
2529
.frame(width: viewModel.size.width / 2.5,
2630
height: viewModel.size.height / 1.5,
2731
alignment: .center)
28-
.padding()
29-
.background(Color.red)
32+
.background(Color.secondary)
3033
.cornerRadius(4.0)
3134
.offset(x: 0,
3235
y: viewModel.isVisible ? ((viewModel.size.height / 2) - ((viewModel.size.height / 1.5) / 1.5)) : 10000)
3336
.transition(AnyTransition.move(edge: .bottom))
3437
}
3538
}
3639

40+
var firstView: some View {
41+
VStack {
42+
Spacer()
43+
Text("Test 1")
44+
Spacer()
45+
HStack {
46+
Spacer()
47+
Button(action: {
48+
self.currentPage += 1
49+
}) {
50+
Text("Next")
51+
.padding(EdgeInsets(top: 8, leading: 20, bottom: 8, trailing: 20))
52+
.background(Color.accent)
53+
.cornerRadius(4)
54+
}
55+
.buttonStyle(PlainButtonStyle())
56+
}
57+
}
58+
.padding()
59+
}
60+
61+
var secondView: some View {
62+
VStack {
63+
Spacer()
64+
Text("Test 2")
65+
Spacer()
66+
}
67+
.padding()
68+
}
69+
3770
var backgroundView: some View {
3871
viewModel.isVisible ? Color.black.opacity(0.8) : Color.clear
3972
}

Snip/SnipViewApp.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ struct SnipViewApp: View {
7878

7979
var welcomePanel: some View {
8080
GeometryReader { reader in
81-
WelcomeView(viewModel: WelcomeViewModel(isVisible: false, readerSize: reader.size))
81+
WelcomeView(viewModel: WelcomeViewModel(isVisible: true, readerSize: reader.size))
8282
}
8383
}
8484

0 commit comments

Comments
 (0)