Skip to content

Commit

Permalink
Merge pull request #20 from fermoya/fix/pager-swiping
Browse files Browse the repository at this point in the history
Fix/pager swiping
  • Loading branch information
fermoya authored Mar 2, 2020
2 parents 71fcbb1 + 91374fe commit 24841bb
Show file tree
Hide file tree
Showing 12 changed files with 343 additions and 122 deletions.
26 changes: 24 additions & 2 deletions Example/SwiftUIPagerExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
17D9E0F423D4CF6700C5AE93 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17D9E0F323D4CF6700C5AE93 /* AppDelegate.swift */; };
17D9E0F623D4CF6700C5AE93 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17D9E0F523D4CF6700C5AE93 /* SceneDelegate.swift */; };
17D9E0F823D4CF6700C5AE93 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17D9E0F723D4CF6700C5AE93 /* ContentView.swift */; };
17D9E0FA23D4CF6900C5AE93 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 17D9E0F923D4CF6900C5AE93 /* Assets.xcassets */; };
17D9E0FD23D4CF6900C5AE93 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 17D9E0FC23D4CF6900C5AE93 /* Assets.xcassets */; };
17D9E10023D4CF6900C5AE93 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 17D9E0FE23D4CF6900C5AE93 /* LaunchScreen.storyboard */; };
6B4EC8A2240D072B001E7490 /* ColorsExampleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B4EC8A1240D072B001E7490 /* ColorsExampleView.swift */; };
6B4EC8A4240D07D5001E7490 /* SimpleExampleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B4EC8A3240D07D5001E7490 /* SimpleExampleView.swift */; };
6B4EC8A6240D0918001E7490 /* PresentedExampleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B4EC8A5240D0918001E7490 /* PresentedExampleView.swift */; };
6B4EC8A8240D1182001E7490 /* BizarreExampleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B4EC8A7240D1182001E7490 /* BizarreExampleView.swift */; };
6BBBB7D32404367C0058EE56 /* PositionAlignment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6BBBB7D22404367C0058EE56 /* PositionAlignment.swift */; };
6BD68FFA23F59CDE00A518D3 /* Pager+Buildable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6BD68FF323F59CDD00A518D3 /* Pager+Buildable.swift */; };
6BD68FFB23F59CDE00A518D3 /* Pager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6BD68FF423F59CDD00A518D3 /* Pager.swift */; };
Expand Down Expand Up @@ -44,6 +47,10 @@
17D9E0FC23D4CF6900C5AE93 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; name = Assets.xcassets; path = ../Assets.xcassets; sourceTree = "<group>"; };
17D9E0FF23D4CF6900C5AE93 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
17D9E10123D4CF6900C5AE93 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
6B4EC8A1240D072B001E7490 /* ColorsExampleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorsExampleView.swift; sourceTree = "<group>"; };
6B4EC8A3240D07D5001E7490 /* SimpleExampleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SimpleExampleView.swift; sourceTree = "<group>"; };
6B4EC8A5240D0918001E7490 /* PresentedExampleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PresentedExampleView.swift; sourceTree = "<group>"; };
6B4EC8A7240D1182001E7490 /* BizarreExampleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BizarreExampleView.swift; sourceTree = "<group>"; };
6BBBB7D22404367C0058EE56 /* PositionAlignment.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PositionAlignment.swift; sourceTree = "<group>"; };
6BD68FF323F59CDD00A518D3 /* Pager+Buildable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "Pager+Buildable.swift"; path = "../../../Sources/SwiftUIPager/Pager+Buildable.swift"; sourceTree = "<group>"; };
6BD68FF423F59CDD00A518D3 /* Pager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Pager.swift; path = ../../../Sources/SwiftUIPager/Pager.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -83,6 +90,7 @@
17D9E0F223D4CF6700C5AE93 /* SwiftUIPagerExample */ = {
isa = PBXGroup;
children = (
6B4EC8A0240D0710001E7490 /* Examples */,
6BD68FF223F59CCD00A518D3 /* Pagination */,
17D9E0F323D4CF6700C5AE93 /* AppDelegate.swift */,
17D9E0F523D4CF6700C5AE93 /* SceneDelegate.swift */,
Expand All @@ -103,6 +111,17 @@
path = "Preview Content";
sourceTree = "<group>";
};
6B4EC8A0240D0710001E7490 /* Examples */ = {
isa = PBXGroup;
children = (
6B4EC8A1240D072B001E7490 /* ColorsExampleView.swift */,
6B4EC8A3240D07D5001E7490 /* SimpleExampleView.swift */,
6B4EC8A5240D0918001E7490 /* PresentedExampleView.swift */,
6B4EC8A7240D1182001E7490 /* BizarreExampleView.swift */,
);
path = Examples;
sourceTree = "<group>";
};
6BD68FF223F59CCD00A518D3 /* Pagination */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -191,7 +210,6 @@
files = (
17D9E10023D4CF6900C5AE93 /* LaunchScreen.storyboard in Resources */,
17D9E0FD23D4CF6900C5AE93 /* Assets.xcassets in Resources */,
17D9E0FA23D4CF6900C5AE93 /* Assets.xcassets in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -202,12 +220,16 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
6B4EC8A2240D072B001E7490 /* ColorsExampleView.swift in Sources */,
17D9E0F423D4CF6700C5AE93 /* AppDelegate.swift in Sources */,
6BD68FFF23F59CDE00A518D3 /* SizeViewModifier.swift in Sources */,
6BBBB7D32404367C0058EE56 /* PositionAlignment.swift in Sources */,
6B4EC8A6240D0918001E7490 /* PresentedExampleView.swift in Sources */,
6BD68FFB23F59CDE00A518D3 /* Pager.swift in Sources */,
6BD68FFD23F59CDE00A518D3 /* View+Helper.swift in Sources */,
6B4EC8A8240D1182001E7490 /* BizarreExampleView.swift in Sources */,
17D9E0F623D4CF6700C5AE93 /* SceneDelegate.swift in Sources */,
6B4EC8A4240D07D5001E7490 /* SimpleExampleView.swift in Sources */,
6BD68FFA23F59CDE00A518D3 /* Pager+Buildable.swift in Sources */,
17D9E0F823D4CF6700C5AE93 /* ContentView.swift in Sources */,
6BD68FFE23F59CDE00A518D3 /* Buildable.swift in Sources */,
Expand Down
107 changes: 20 additions & 87 deletions Example/SwiftUIPagerExample/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,95 +13,28 @@ extension Color {
}

struct ContentView: View {

@State var isPresented: Bool = false
@State var pageIndex: Int = 0

var colors: [Color] = [
.red, .blue, .black, .gray, .purple, .green, .orange, .pink, .yellow, .white
]

// var body: some View {
// NavigationView {
// GeometryReader { proxy in
// VStack {
// Pager(data: Array(0..<10),
// id: \.self) {
// self.pageView($0)
// }
// .itemSpacing(10)
// .itemAspectRatio(0.8, alignment: .end)
// .padding(8)
// .frame(width: min(proxy.size.height, proxy.size.width),
// height: min(proxy.size.height, proxy.size.width))
// .background(Color.gray.opacity(0.2))
// }
// }.navigationBarTitle("SwiftUIPager", displayMode: .inline)
// }
// }

@State var tab: Int = 0

var body: some View {
NavigationView {
GeometryReader { proxy in
VStack {
Pager(data: self.colors,
id: \.self) {
self.pageView($0)
}
.itemSpacing(10)
.padding(20)
.onPageChanged({ page in
withAnimation {
self.pageIndex = page
}
})
.frame(width: min(proxy.size.height, proxy.size.width),
height: min(proxy.size.height, proxy.size.width))
.background(Color.gray.opacity(0.3))
.navigationBarTitle("Color Picker", displayMode: .inline)

Spacer()

HStack {
Spacer()
Circle()
.fill(self.colors[self.pageIndex])
.frame(width: 80)
.overlay(Circle().stroke(self.pageIndex < 4 ? Color.gray.opacity(0.5) : Color.black, lineWidth: 5))
Spacer()
Text("\(self.colors[self.pageIndex].rgb)")
Spacer()
}
}
}
TabView {
SimpleExampleView()
.tabItem({
Text("Basic")
}).tag(0)
ColorsExampleView()
.tabItem({
Text("Colors")
}).tag(1)
PresentedExampleView()
.tabItem({
Text("Presented")
}).tag(2)
BizarreExampleView()
.tabItem({
Text("More")
}).tag(3)
}
}

}

extension ContentView {

// func pageView(_ page: Int) -> some View {
// ZStack {
// Rectangle()
// .fill(Color.yellow)
// Text("Page: \(page)")
// .bold()
// }
// .cornerRadius(5)
// .shadow(radius: 5)
// }

func pageView(_ color: Color) -> some View {
Rectangle()
.fill(color)
.cornerRadius(5)
.shadow(radius: 5)
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}

}
57 changes: 57 additions & 0 deletions Example/SwiftUIPagerExample/Examples/BizarreExampleView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// VerticalExampleView.swift
// SwiftUIPagerExample
//
// Created by Fernando Moya de Rivas on 02/03/2020.
// Copyright © 2020 Fernando Moya de Rivas. All rights reserved.
//

import SwiftUI

struct BizarreExampleView: View {
@State var page1: Int = 0
@State var page2: Int = 0
@State var data = Array(0..<10)

var body: some View {
GeometryReader { proxy in
VStack(spacing: 10) {
Text("Vertical").bold()
Pager(page: self.$page1,
data: self.data,
id: \.self) {
self.pageView($0)
}
.vertical()
.itemSpacing(10)
.itemAspectRatio(1.3)
.background(Color.gray.opacity(0.2))

Spacer()

Text("Right to left").bold()
Pager(page: self.$page2,
data: self.data,
id: \.self) {
self.pageView($0)
}
.itemSpacing(10)
.horizontal(.rightToLeft)
.interactive(0.8)
.itemAspectRatio(0.7)
.background(Color.gray.opacity(0.5))
}
}
}

func pageView(_ page: Int) -> some View {
ZStack {
Rectangle()
.fill(Color.yellow)
Text("Page: \(page)")
.bold()
}
.cornerRadius(5)
.shadow(radius: 5)
}
}
96 changes: 96 additions & 0 deletions Example/SwiftUIPagerExample/Examples/ColorsExampleView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// ColorsExampleView.swift
// SwiftUIPagerExample
//
// Created by Fernando Moya de Rivas on 02/03/2020.
// Copyright © 2020 Fernando Moya de Rivas. All rights reserved.
//

import SwiftUI

struct ColorsExampleView: View {

@State var pageIndex = 0

var colors: [Color] = [
.red, .blue, .black, .gray, .purple, .green, .orange, .pink, .yellow, .white
]

var body: some View {
NavigationView {
GeometryReader { proxy in
VStack {
Pager(page: self.$pageIndex,
data: self.colors,
id: \.self) {
self.pageView($0)
}
.itemSpacing(10)
.padding(20)
.onPageChanged({ page in
withAnimation {
self.pageIndex = page
}
})
.frame(width: min(proxy.size.height, proxy.size.width),
height: min(proxy.size.height, proxy.size.width))
.background(Color.gray.opacity(0.3))
.navigationBarTitle("Color Picker", displayMode: .inline)

Spacer()

HStack {
Spacer()
Circle()
.fill(self.colors[self.pageIndex])
.frame(width: 80)
.overlay(Circle().stroke(self.pageIndex < 4 ? Color.gray.opacity(0.5) : Color.black, lineWidth: 5))
Spacer()
Text("\(self.colors[self.pageIndex].rgb)")
Spacer()
}

Spacer()

HStack {
Spacer()
Button(action: {
withAnimation {
self.pageIndex = max(0, self.pageIndex - 1)
}
}, label: {
HStack(spacing: 10) {
Image(systemName: "backward.fill")
.padding()
Text("Previous")
}
}).disabled(self.pageIndex <= 0)
Spacer()
Button(action: {
withAnimation {
self.pageIndex = min(self.colors.count - 1, self.pageIndex + 1)
}
}, label: {
HStack(spacing: 10) {
Text("Next")
Image(systemName: "forward.fill")
.padding()
}
}).disabled(self.pageIndex >= self.colors.count - 1)
Spacer()
}

Spacer()
}
}
}
}

func pageView(_ color: Color) -> some View {
Rectangle()
.fill(color)
.cornerRadius(5)
.shadow(radius: 5)
}

}
Loading

0 comments on commit 24841bb

Please sign in to comment.