Skip to content

Commit

Permalink
Merge pull request #1 from fredyshox/performance
Browse files Browse the repository at this point in the history
Performance improvement
  • Loading branch information
fredyshox authored Mar 20, 2020
2 parents cf62c0d + 969c67a commit 32bcb64
Show file tree
Hide file tree
Showing 9 changed files with 415 additions and 153 deletions.
34 changes: 16 additions & 18 deletions Examples/PageViewDemo WatchKit Extension/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,23 @@ import SwiftUI
struct ContentView: View {
var body: some View {
// Horizontal
PageView(axis: .horizontal, pageCount: 3) { (i) -> AnyView in
if i == 0 {
return CustomView().eraseToAnyView()
} else if i == 1 {
return CustomListView().eraseToAnyView()
} else {
return CustomView().eraseToAnyView()
}
}.edgesIgnoringSafeArea([.leading, .trailing, .bottom])
HPageView {
CustomView()
CustomListView()
CustomView()
CustomView()
CustomView()
CustomView()
}.edgesIgnoringSafeArea(.init(arrayLiteral: .leading, .trailing, .bottom))
// Vertical
// PageView(axis: .vertical(alignment: Alignment(horizontal: .trailing, vertical: .top)), pageCount: 3) { (i) -> AnyView in
// if i == 0 {
// return CustomView().eraseToAnyView()
// } else if i == 1 {
// return CustomView().eraseToAnyView()
// } else {
// return CustomView().eraseToAnyView()
// }
// }.edgesIgnoringSafeArea([.leading, .trailing, .bottom])
// VPageView {
// CustomView()
// CustomListView()
// CustomView()
// CustomView()
// CustomView()
// CustomView()
// }.edgesIgnoringSafeArea(.init(arrayLiteral: .leading, .trailing, .bottom))
}
}

Expand Down
6 changes: 6 additions & 0 deletions Examples/PageViewDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
E211E07323F30C49000EB7DA /* PageScrollState.swift in Sources */ = {isa = PBXBuildFile; fileRef = E211E07023F30C49000EB7DA /* PageScrollState.swift */; };
E211E07423F30C49000EB7DA /* PageContent.swift in Sources */ = {isa = PBXBuildFile; fileRef = E211E07123F30C49000EB7DA /* PageContent.swift */; };
E211E07523F30C49000EB7DA /* PageContent.swift in Sources */ = {isa = PBXBuildFile; fileRef = E211E07123F30C49000EB7DA /* PageContent.swift */; };
E2B0F9CE242568510065DFBE /* PageViewBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2B0F9CD242568510065DFBE /* PageViewBuilder.swift */; };
E2B0F9CF2425685D0065DFBE /* PageViewBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2B0F9CD242568510065DFBE /* PageViewBuilder.swift */; };
E711489123EF7D14009ABC2D /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E711489023EF7D14009ABC2D /* AppDelegate.swift */; };
E711489323EF7D14009ABC2D /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E711489223EF7D14009ABC2D /* SceneDelegate.swift */; };
E711489523EF7D14009ABC2D /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E711489423EF7D14009ABC2D /* ContentView.swift */; };
Expand Down Expand Up @@ -86,6 +88,7 @@
E211E06523F219F9000EB7DA /* PageControlTheme.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = PageControlTheme.swift; path = ../Sources/PageControlTheme.swift; sourceTree = "<group>"; };
E211E07023F30C49000EB7DA /* PageScrollState.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = PageScrollState.swift; path = ../Sources/PageScrollState.swift; sourceTree = "<group>"; };
E211E07123F30C49000EB7DA /* PageContent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = PageContent.swift; path = ../Sources/PageContent.swift; sourceTree = "<group>"; };
E2B0F9CD242568510065DFBE /* PageViewBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = PageViewBuilder.swift; path = ../Sources/PageViewBuilder.swift; sourceTree = "<group>"; };
E711488D23EF7D14009ABC2D /* PageViewDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = PageViewDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
E711489023EF7D14009ABC2D /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
E711489223EF7D14009ABC2D /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -134,6 +137,7 @@
E211E06423F219F9000EB7DA /* PageView.swift */,
E211E07123F30C49000EB7DA /* PageContent.swift */,
E211E07023F30C49000EB7DA /* PageScrollState.swift */,
E2B0F9CD242568510065DFBE /* PageViewBuilder.swift */,
);
name = PageView;
sourceTree = "<group>";
Expand Down Expand Up @@ -363,6 +367,7 @@
buildActionMask = 2147483647;
files = (
E211E06623F219F9000EB7DA /* PageControl.swift in Sources */,
E2B0F9CE242568510065DFBE /* PageViewBuilder.swift in Sources */,
E211E06723F219F9000EB7DA /* PageView.swift in Sources */,
E71148CF23EF7D38009ABC2D /* Views.swift in Sources */,
E711489123EF7D14009ABC2D /* AppDelegate.swift in Sources */,
Expand All @@ -379,6 +384,7 @@
buildActionMask = 2147483647;
files = (
E71148D023EF7E73009ABC2D /* Views.swift in Sources */,
E2B0F9CF2425685D0065DFBE /* PageViewBuilder.swift in Sources */,
E71148B823EF7D16009ABC2D /* HostingController.swift in Sources */,
E211E06A23F21A25000EB7DA /* PageControl.swift in Sources */,
E71148B623EF7D16009ABC2D /* ContentView.swift in Sources */,
Expand Down
12 changes: 4 additions & 8 deletions Examples/PageViewDemo/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@ import SwiftUI

struct ContentView: View {
var body: some View {
PageView(pageCount: 3) { (i) -> AnyView in
if i == 0 {
return CustomView().eraseToAnyView()
} else if i == 1 {
return CustomListView().eraseToAnyView()
} else {
return CustomView().eraseToAnyView()
}
HPageView {
CustomView()
CustomListView()
CustomView()
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions PageView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
/* Begin PBXBuildFile section */
E211E06D23F30AD5000EB7DA /* PageContent.swift in Sources */ = {isa = PBXBuildFile; fileRef = E211E06C23F30AD5000EB7DA /* PageContent.swift */; };
E211E06F23F30AFD000EB7DA /* PageScrollState.swift in Sources */ = {isa = PBXBuildFile; fileRef = E211E06E23F30AFD000EB7DA /* PageScrollState.swift */; };
E2B0F9CC242561620065DFBE /* PageViewBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2B0F9CB242561620065DFBE /* PageViewBuilder.swift */; };
OBJ_23 /* PageControl.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_8 /* PageControl.swift */; };
OBJ_24 /* PageControlTheme.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_9 /* PageControlTheme.swift */; };
OBJ_25 /* PageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_10 /* PageView.swift */; };
Expand Down Expand Up @@ -52,6 +53,7 @@
/* Begin PBXFileReference section */
E211E06C23F30AD5000EB7DA /* PageContent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PageContent.swift; sourceTree = "<group>"; };
E211E06E23F30AFD000EB7DA /* PageScrollState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PageScrollState.swift; sourceTree = "<group>"; };
E2B0F9CB242561620065DFBE /* PageViewBuilder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PageViewBuilder.swift; sourceTree = "<group>"; };
OBJ_10 /* PageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PageView.swift; sourceTree = "<group>"; };
OBJ_13 /* PageViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PageViewTests.swift; sourceTree = "<group>"; };
OBJ_14 /* XCTestManifests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XCTestManifests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -126,6 +128,7 @@
OBJ_10 /* PageView.swift */,
E211E06C23F30AD5000EB7DA /* PageContent.swift */,
E211E06E23F30AFD000EB7DA /* PageScrollState.swift */,
E2B0F9CB242561620065DFBE /* PageViewBuilder.swift */,
);
path = Sources;
sourceTree = SOURCE_ROOT;
Expand Down Expand Up @@ -217,6 +220,7 @@
E211E06D23F30AD5000EB7DA /* PageContent.swift in Sources */,
OBJ_23 /* PageControl.swift in Sources */,
E211E06F23F30AFD000EB7DA /* PageScrollState.swift in Sources */,
E2B0F9CC242561620065DFBE /* PageViewBuilder.swift in Sources */,
OBJ_24 /* PageControlTheme.swift in Sources */,
OBJ_25 /* PageView.swift in Sources */,
);
Expand Down Expand Up @@ -269,7 +273,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) $(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 1.2.0;
MARKETING_VERSION = 1.3.0;
OTHER_CFLAGS = "$(inherited)";
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
Expand Down Expand Up @@ -299,7 +303,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) $(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 1.2.0;
MARKETING_VERSION = 1.3.0;
OTHER_CFLAGS = "$(inherited)";
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
Expand Down
47 changes: 19 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ Package requires iOS 13, watchOS 6 and Xcode 11.

For Swift Package Manager add the following package to your Package.swift:
```swift
.package(url: "https://github.com/fredyshox/PageView.git", .upToNextMajor(from: "1.2.0")),
.package(url: "https://github.com/fredyshox/PageView.git", .upToNextMajor(from: "1.3.0")),
```

### Carthage


Carthage is also supported, add FormView by adding to Cartfile:
```
github "fredyshox/PageView" ~> 1.2.0
github "fredyshox/PageView" ~> 1.3.0
```

## Demo
Expand All @@ -39,36 +39,25 @@ Demo app for both iOS and watchOS is provided in `Examples/` directory.
import PageView
```

Add paged view with 3 pages using following code:
PageView component is available as `HPageView` or `VPageView` depending on scroll direction (horizontal and vertical, respectively).
To add paged view with 3 pages use following code:
```swift
PageView(pageCount: 3) { pageIndex in
if pageIndex == 0 || pageIndex == 1 {
return AnyView(SomeCustomView())
} else {
return AnyView(AnotherCustomView())
}
// horizontal axis
HPageView {
SomeCustomView()
AnotherCustomView()
AnotherCustomView()
}
```

By default `PageView` fills all the available area, you can constrain it's size using `.frame(width:, height:)` View modifier.

Paging axis can be specified using `axis:` parameter. PageAxis can be `.vertical` or `.horizontal`. By default `PageView` assumes `.horizontal` axis.

```swift
PageView(axis: .vertical, pageCount: 4) { pageIndex in
...
}
```

Alignment of page control can be specified using `alignment:` enum parameter of PageAxis. For example, to achieve top-trailing alignment with vertical axis:
```swift
let axis: PageAxis = .vertical(alignment: .topTrailing)
PageView(axis: axis, pageCount: 5) {
...
// vertical axis
VPageView {
SomeCustomView()
AnotherCustomView()
AnotherCustomView()
}
```

Default alignment is bottom-center for horizontal axis and center-leading for vertical.
By default PageView fills all the available area, you can constrain it's size using `.frame(width:, height:)` View modifier.

You can customize the styling of page control component by passing `PageControlTheme`. Customizable properties:
* background color
Expand All @@ -78,6 +67,7 @@ You can customize the styling of page control component by passing `PageControlT
* spacing between dots
* padding of page control
* page control offset
* alignment of page control component (default: bottom-center for horizontal axis, center-leading for vertical axis)

```swift
let theme = PageControlTheme(
Expand All @@ -87,10 +77,11 @@ let theme = PageControlTheme(
dotSize: 10.0,
spacing: 12.0,
padding: 5.0,
offset: 8.0
offset: 8.0,
alignment: Alignment(horizontal: .trailing, vertical: .top)
)
...
PageView(theme: theme, pageCount: 4) { pageIndex in
VPageView(theme: theme) {
...
}
```
Expand Down
Loading

0 comments on commit 32bcb64

Please sign in to comment.