├── Tests └── PagesTests │ ├── PagesTests.swift │ └── ModelPagesTests.swift ├── .travis.yml ├── Package.swift ├── .gitignore ├── LICENSE ├── Sources └── Pages │ ├── PageControl.swift │ ├── Pages.swift │ ├── PagesBuilder.swift │ ├── ModelPages.swift │ └── PageViewController.swift └── README.md /Tests/PagesTests/PagesTests.swift: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | osx_image: xcode11.2 2 | language: swift 3 | script: 4 | - xcodebuild test -destination 'name=iPhone 11' -scheme 'Pages' 5 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.1 2 | 3 | import PackageDescription 4 | 5 | let package = Package( 6 | name: "Pages", 7 | platforms: [ 8 | .iOS(.v13) 9 | ], 10 | products: [ 11 | .library( 12 | name: "Pages", 13 | targets: ["Pages"]), 14 | ], 15 | dependencies: [], 16 | targets: [ 17 | .target( 18 | name: "Pages", 19 | dependencies: []), 20 | .testTarget( 21 | name: "PagesTests", 22 | dependencies: ["Pages"] 23 | ) 24 | ] 25 | ) 26 | -------------------------------------------------------------------------------- /Tests/PagesTests/ModelPagesTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // File.swift 3 | // 4 | // 5 | // Created by Nacho Navarro on 08/11/2019. 6 | // 7 | 8 | import XCTest 9 | import SwiftUI 10 | @testable import Pages 11 | 12 | final class ModelPagesTests: XCTestCase { 13 | 14 | struct Car: Identifiable { 15 | var id = UUID() 16 | var model: String 17 | } 18 | 19 | let cars = [Car(model: "Ford"), Car(model: "Ferrari")] 20 | 21 | func testDynamicPagesCount() { 22 | let modelPages = ModelPages(cars, currentPage: .constant(0)) { i, car in 23 | Text("Car model: \(car.model)") 24 | } 25 | XCTAssertEqual(modelPages.items.count, cars.count) 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | 3 | # Xcode 4 | # 5 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 6 | 7 | ## Build generated 8 | build/ 9 | DerivedData/ 10 | 11 | ## Include Demo for local testing 12 | Demo/ 13 | 14 | ## Various settings 15 | *.pbxuser 16 | !default.pbxuser 17 | *.mode1v3 18 | !default.mode1v3 19 | *.mode2v3 20 | !default.mode2v3 21 | *.perspectivev3 22 | !default.perspectivev3 23 | xcuserdata/ 24 | 25 | ## Other 26 | *.moved-aside 27 | *.xccheckout 28 | *.xcscmblueprint 29 | 30 | ## Obj-C/Swift specific 31 | *.hmap 32 | *.ipa 33 | *.dSYM.zip 34 | *.dSYM 35 | 36 | # Swift Package Manager 37 | Packages/ 38 | Package.pins 39 | Package.resolved 40 | .build/ 41 | .build 42 | /.previous-build 43 | xcuserdata 44 | *.xcscmblueprint 45 | /default.profraw 46 | *.xcodeproj 47 | .swiftpm 48 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Nacho Navarro 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Sources/Pages/PageControl.swift: -------------------------------------------------------------------------------- 1 | // 2 | // PageControl.swift 3 | // Pages 4 | // 5 | // Created by Nacho Navarro on 03/11/2019. 6 | // Copyright © 2019 nachonavarro. All rights reserved. 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | 26 | import SwiftUI 27 | import UIKit 28 | 29 | /// The page control for the page view. 30 | @available(iOS 13.0, *) 31 | internal struct PageControl: UIViewRepresentable { 32 | var numberOfPages: Int 33 | var pageControl: UIPageControl? 34 | var currentPageIndicatorTintColor: UIColor 35 | var pageIndicatorTintColor: UIColor 36 | @Binding var currentPage: Int 37 | 38 | func makeCoordinator() -> PageControlCoordinator { 39 | PageControlCoordinator(self) 40 | } 41 | 42 | func makeUIView(context: Context) -> UIPageControl { 43 | if let control = self.pageControl { 44 | return control 45 | } else { 46 | let control = UIPageControl() 47 | control.numberOfPages = numberOfPages 48 | control.currentPageIndicatorTintColor = currentPageIndicatorTintColor 49 | control.pageIndicatorTintColor = pageIndicatorTintColor 50 | control.addTarget( 51 | context.coordinator, 52 | action: #selector(Coordinator.updateCurrentPage(sender:)), 53 | for: .valueChanged 54 | ) 55 | return control 56 | } 57 | } 58 | 59 | func updateUIView(_ uiView: UIPageControl, context: Context) { 60 | uiView.currentPage = self.currentPage 61 | uiView.numberOfPages = self.numberOfPages 62 | } 63 | 64 | } 65 | 66 | @available(iOS 13.0, *) 67 | class PageControlCoordinator: NSObject { 68 | var control: PageControl 69 | 70 | init(_ control: PageControl) { 71 | self.control = control 72 | } 73 | 74 | @objc 75 | func updateCurrentPage(sender: UIPageControl) { 76 | control.currentPage = sender.currentPage 77 | } 78 | } 79 | 80 | -------------------------------------------------------------------------------- /Sources/Pages/Pages.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Pages.swift 3 | // Pages 4 | // 5 | // Created by Nacho Navarro on 03/11/2019. 6 | // Copyright © 2019 nachonavarro. All rights reserved. 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | 26 | import SwiftUI 27 | import UIKit 28 | 29 | /// A paging view that generates user-defined static pages. 30 | @available(iOS 13.0, *) 31 | public struct Pages: View { 32 | 33 | @Binding var currentPage: Int 34 | var pages: [AnyView] 35 | 36 | var navigationOrientation: UIPageViewController.NavigationOrientation 37 | var transitionStyle: UIPageViewController.TransitionStyle 38 | var bounce: Bool 39 | var wrap: Bool 40 | var hasControl: Bool 41 | var pageControl: UIPageControl? = nil 42 | var controlAlignment: Alignment 43 | var currentTintColor: UIColor 44 | var tintColor: UIColor 45 | 46 | /** 47 | Creates the paging view that generates user-defined static pages. 48 | 49 | `Pages` can be used as follows: 50 | ``` 51 | struct WelcomeView: View { 52 | 53 | @State var index: Int = 0 54 | 55 | var body: some View { 56 | Pages(currentPage: $index) { 57 | Text("Welcome! This is Page 1") 58 | Text("This is Page 2") 59 | Text("...and this is Page 3") 60 | } 61 | } 62 | } 63 | ``` 64 | 65 | - Parameters: 66 | - navigationOrientation: Whether to paginate horizontally or vertically. 67 | - transitionStyle: Whether to perform a page curl or a scroll effect on page turn. 68 | - bounce: Whether to bounce back when a user tries to scroll past all the pages. 69 | - wrap: A flag indicating whether to wrap the pages circularly when the user scrolls past the beginning or end. 70 | - hasControl: Whether to display a page control or not. 71 | - control: A user defined page control. 72 | - controlAlignment: What position to put the page control. 73 | - pages: A function builder `PagesBuilder` that will put the views defined by the user on a list. 74 | */ 75 | public init( 76 | currentPage: Binding, 77 | navigationOrientation: UIPageViewController.NavigationOrientation = .horizontal, 78 | transitionStyle: UIPageViewController.TransitionStyle = .scroll, 79 | bounce: Bool = true, 80 | wrap: Bool = false, 81 | hasControl: Bool = true, 82 | control: UIPageControl? = nil, 83 | controlAlignment: Alignment = .bottom, 84 | currentTintColor: UIColor = .white, 85 | tintColor: UIColor = .gray, 86 | @PagesBuilder pages: () -> [AnyView] 87 | 88 | ) { 89 | self.navigationOrientation = navigationOrientation 90 | self.transitionStyle = transitionStyle 91 | self.bounce = bounce 92 | self.wrap = wrap 93 | self.hasControl = hasControl 94 | self.pageControl = control 95 | self.controlAlignment = controlAlignment 96 | self.currentTintColor = currentTintColor 97 | self.tintColor = tintColor 98 | self.pages = pages() 99 | self._currentPage = currentPage 100 | 101 | } 102 | 103 | public var body: some View { 104 | ZStack(alignment: self.controlAlignment) { 105 | PageViewController( 106 | currentPage: $currentPage, 107 | navigationOrientation: navigationOrientation, 108 | transitionStyle: transitionStyle, 109 | bounce: bounce, 110 | wrap: wrap, 111 | controllers: pages.map { 112 | let h = UIHostingController(rootView: $0) 113 | h.view.backgroundColor = .clear 114 | return h 115 | } 116 | ) 117 | if self.hasControl { 118 | PageControl( 119 | numberOfPages: pages.count, 120 | pageControl: pageControl, 121 | currentPageIndicatorTintColor: currentTintColor, 122 | pageIndicatorTintColor: tintColor, 123 | currentPage: $currentPage 124 | ).padding() 125 | } 126 | } 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /Sources/Pages/PagesBuilder.swift: -------------------------------------------------------------------------------- 1 | // 2 | // PagesBuilder.swift 3 | // Pages 4 | // 5 | // Created by Nacho Navarro on 03/11/2019. 6 | // Copyright © 2019 nachonavarro. All rights reserved. 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | 26 | import Foundation 27 | import SwiftUI 28 | 29 | /** 30 | A function builder that captures each direct child of `Pages` and adds it to a list. 31 | 32 | This implementation tries to mimic that of `ViewBuilder`. I highly recommed taking a look 33 | at the public interface of `ViewBuilder` to get a sense of how SwiftUI implements it. In their 34 | case they wrap the blocks in `buildBlock` on a `TupleView`. This however means we can't 35 | access each child individually (which is a must in a paging view). In our case we keep the children 36 | separated by storing them on a list of type `[AnyView]` that will allow us to store views of different type 37 | (e.g. a `Text` followed by a `Circle` followed by an `Image`). 38 | 39 | It may look like there's some code duplication, but I have not found a way to reduce it, and in fact I think 40 | SwiftUI does the same. Further information 41 | [here](https://forums.swift.org/t/function-builders/25167) and [here](https://github.com/apple/swift-evolution/blob/9992cf3c11c2d5e0ea20bee98657d93902d5b174/proposals/XXXX-function-builders.md). 42 | */ 43 | @available(iOS 13.0, *) 44 | @_functionBuilder 45 | public struct PagesBuilder { 46 | 47 | 48 | public static func buildBlock( 49 | _ c0: C0) -> [AnyView] { 50 | [AnyView(c0)] 51 | } 52 | 53 | public static func buildBlock(_ c0: C0, _ c1: C1) -> [AnyView] { 54 | [AnyView(c0), AnyView(c1)] 55 | } 56 | 57 | public static func buildBlock( 58 | _ c0: C0, 59 | _ c1: C1, 60 | _ c2: C2) -> [AnyView] { 61 | [AnyView(c0), AnyView(c1), AnyView(c2)] 62 | } 63 | 64 | public static func buildBlock( 65 | _ c0: C0, 66 | _ c1: C1, 67 | _ c2: C2, 68 | _ c3: C3) -> [AnyView] { 69 | [AnyView(c0), AnyView(c1), AnyView(c2), AnyView(c3)] 70 | } 71 | 72 | public static func buildBlock( 73 | _ c0: C0, 74 | _ c1: C1, 75 | _ c2: C2, 76 | _ c3: C3, 77 | _ c4: C4) -> [AnyView] { 78 | [AnyView(c0), AnyView(c1), AnyView(c2), AnyView(c3), AnyView(c4)] 79 | } 80 | 81 | public static func buildBlock( 82 | _ c0: C0, 83 | _ c1: C1, 84 | _ c2: C2, 85 | _ c3: C3, 86 | _ c4: C4, 87 | _ c5: C5) -> [AnyView] { 88 | [AnyView(c0), AnyView(c1), AnyView(c2), AnyView(c3), AnyView(c4), AnyView(c5)] 89 | } 90 | 91 | public static func buildBlock( 92 | _ c0: C0, 93 | _ c1: C1, 94 | _ c2: C2, 95 | _ c3: C3, 96 | _ c4: C4, 97 | _ c5: C5, 98 | _ c6: C6) -> [AnyView] { 99 | [AnyView(c0), AnyView(c1), AnyView(c2), AnyView(c3), AnyView(c4), AnyView(c5), AnyView(c6)] 100 | } 101 | 102 | public static func buildBlock( 103 | _ c0: C0, 104 | _ c1: C1, 105 | _ c2: C2, 106 | _ c3: C3, 107 | _ c4: C4, 108 | _ c5: C5, 109 | _ c6: C6, 110 | _ c7: C7) -> [AnyView] { 111 | [AnyView(c0), AnyView(c1), AnyView(c2), AnyView(c3), AnyView(c4), AnyView(c5), AnyView(c6), AnyView(c7)] 112 | } 113 | 114 | public static func buildBlock( 115 | _ c0: C0, 116 | _ c1: C1, 117 | _ c2: C2, 118 | _ c3: C3, 119 | _ c4: C4, 120 | _ c5: C5, 121 | _ c6: C6, 122 | _ c7: C7, 123 | _ c8: C8) -> [AnyView] { 124 | [AnyView(c0), AnyView(c1), AnyView(c2), AnyView(c3), AnyView(c4), AnyView(c5), AnyView(c6), AnyView(c7), AnyView(c8)] 125 | } 126 | 127 | public static func buildBlock( 128 | _ c0: C0, 129 | _ c1: C1, 130 | _ c2: C2, 131 | _ c3: C3, 132 | _ c4: C4, 133 | _ c5: C5, 134 | _ c6: C6, 135 | _ c7: C7, 136 | _ c8: C8, 137 | _ c9: C9) -> [AnyView] { 138 | [AnyView(c0), AnyView(c1), AnyView(c2), AnyView(c3), AnyView(c4), AnyView(c5), AnyView(c6), AnyView(c7), AnyView(c8), AnyView(c9)] 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /Sources/Pages/ModelPages.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ModelPages.swift 3 | // Pages 4 | // 5 | // Created by Nacho Navarro on 01/11/2019. 6 | // Copyright © 2019 nachonavarro. All rights reserved. 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | 26 | import SwiftUI 27 | import UIKit 28 | 29 | /// A paging view that generates pages dynamically based on some user-defined data. 30 | @available(iOS 13.0, *) 31 | public struct ModelPages: View where Data: RandomAccessCollection, Content: View { 32 | 33 | @Binding var currentPage: Int 34 | var items: [Data.Element] 35 | 36 | private var template: (Int, Data.Element) -> Content 37 | private var navigationOrientation: UIPageViewController.NavigationOrientation 38 | private var transitionStyle: UIPageViewController.TransitionStyle 39 | private var bounce: Bool 40 | private var wrap: Bool 41 | private var hasControl: Bool 42 | private var pageControl: UIPageControl? = nil 43 | private var controlAlignment: Alignment 44 | private var currentTintColor: UIColor 45 | private var tintColor: UIColor 46 | 47 | /** 48 | Creates the paging view that generates pages dynamically based on some user-defined data. 49 | 50 | `ModelPages` can be used as follows: 51 | ``` 52 | struct Car: { 53 | var model: String 54 | } 55 | 56 | struct CarsView: View { 57 | 58 | @State var index: Int = 0 59 | let cars = [Car(model: "Ford"), Car(model: "Ferrari") 60 | 61 | var body: some View { 62 | ModelPages(self.cars, currentPage: $index) { i, car in 63 | Text("Car is \(car.model)!") 64 | } 65 | } 66 | } 67 | ``` 68 | 69 | - Parameters: 70 | - items: The collection of data that will drive page creation. 71 | - currentPage: A binding to give the user control over the current page index. 72 | - navigationOrientation: Whether to paginate horizontally or vertically. 73 | - transitionStyle: Whether to perform a page curl or a scroll effect on page turn. 74 | - bounce: Whether to bounce back when a user tries to scroll past all the pages. 75 | - wrap: A flag indicating whether to wrap the pages circularly when the user scrolls past the beginning or end. 76 | - hasControl: Whether to display a page control or not. 77 | - control: A user defined page control. 78 | - controlAlignment: What position to put the page control. 79 | - template: A function that specifies how a page looks like given the position of the page and the item related to the page. 80 | */ 81 | public init( 82 | _ items: Data, 83 | currentPage: Binding, 84 | navigationOrientation: UIPageViewController.NavigationOrientation = .horizontal, 85 | transitionStyle: UIPageViewController.TransitionStyle = .scroll, 86 | bounce: Bool = true, 87 | wrap: Bool = false, 88 | hasControl: Bool = true, 89 | control: UIPageControl? = nil, 90 | controlAlignment: Alignment = .bottom, 91 | currentTintColor: UIColor = .white, 92 | tintColor: UIColor = .gray, 93 | template: @escaping (Int, Data.Element) -> Content 94 | 95 | ) { 96 | self._currentPage = currentPage 97 | self.navigationOrientation = navigationOrientation 98 | self.transitionStyle = transitionStyle 99 | self.bounce = bounce 100 | self.wrap = wrap 101 | self.hasControl = hasControl 102 | self.pageControl = control 103 | self.controlAlignment = controlAlignment 104 | self.items = items.map { $0 } 105 | self.template = template 106 | self.currentTintColor = currentTintColor 107 | self.tintColor = tintColor 108 | } 109 | 110 | public var body: some View { 111 | ZStack(alignment: self.controlAlignment) { 112 | PageViewController( 113 | currentPage: $currentPage, 114 | navigationOrientation: navigationOrientation, 115 | transitionStyle: transitionStyle, 116 | bounce: bounce, 117 | wrap: wrap, 118 | controllers: (0.. PagesCoordinator { 42 | PagesCoordinator(self) 43 | } 44 | 45 | func makeUIViewController(context: Context) -> UIPageViewController { 46 | let pageViewController = UIPageViewController( 47 | transitionStyle: self.transitionStyle, 48 | navigationOrientation: self.navigationOrientation 49 | ) 50 | pageViewController.dataSource = context.coordinator 51 | pageViewController.delegate = context.coordinator 52 | pageViewController.view.backgroundColor = .clear 53 | 54 | for view in pageViewController.view.subviews { 55 | if let scrollView = view as? UIScrollView { 56 | scrollView.delegate = context.coordinator 57 | break 58 | } 59 | } 60 | 61 | return pageViewController 62 | } 63 | 64 | func updateUIViewController(_ pageViewController: UIPageViewController, context: Context) { 65 | let previousPage = context.coordinator.parent.currentPage 66 | context.coordinator.parent = self 67 | 68 | if currentPage == previousPage, 69 | pageViewController.viewControllers != nil, 70 | pageViewController.viewControllers?.count ?? 0 > 0 { 71 | return 72 | } 73 | pageViewController.setViewControllers( 74 | [controllers[currentPage]], 75 | direction: currentPage - previousPage > 0 ? .forward : .reverse, 76 | animated: false 77 | ) 78 | } 79 | 80 | } 81 | 82 | @available(iOS 13.0, *) 83 | class PagesCoordinator: NSObject, UIPageViewControllerDataSource, 84 | UIPageViewControllerDelegate { 85 | var parent: PageViewController 86 | 87 | init(_ pageViewController: PageViewController) { 88 | self.parent = pageViewController 89 | } 90 | 91 | func pageViewController( 92 | _ pageViewController: UIPageViewController, 93 | viewControllerBefore viewController: UIViewController 94 | ) -> UIViewController? { 95 | guard let index = parent.controllers.firstIndex(of: viewController) else { 96 | return nil 97 | } 98 | return index == 0 ? (self.parent.wrap ? parent.controllers.last : nil) : parent.controllers[index - 1] 99 | } 100 | 101 | func pageViewController( 102 | _ pageViewController: UIPageViewController, 103 | viewControllerAfter viewController: UIViewController 104 | ) -> UIViewController? { 105 | guard let index = parent.controllers.firstIndex(of: viewController) else { 106 | return nil 107 | } 108 | return index == parent.controllers.count - 1 ? (self.parent.wrap ? parent.controllers.first : nil) : parent.controllers[index + 1] 109 | } 110 | 111 | func pageViewController( 112 | _ pageViewController: UIPageViewController, 113 | didFinishAnimating finished: Bool, 114 | previousViewControllers: [UIViewController], 115 | transitionCompleted completed: Bool 116 | ) { 117 | if completed, 118 | let visibleViewController = pageViewController.viewControllers?.first, 119 | let index = parent.controllers.firstIndex(of: visibleViewController) { 120 | parent.currentPage = index 121 | } 122 | } 123 | } 124 | 125 | @available(iOS 13.0, *) 126 | extension PagesCoordinator: UIScrollViewDelegate { 127 | 128 | func scrollViewDidScroll(_ scrollView: UIScrollView) { 129 | if !parent.bounce { 130 | if parent.navigationOrientation == .horizontal { 131 | disableHorizontalBounce(scrollView) 132 | } else { 133 | disableVerticalBounce(scrollView) 134 | } 135 | } 136 | } 137 | 138 | func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer) { 139 | scrollViewDidScroll(scrollView) 140 | } 141 | 142 | private func disableHorizontalBounce(_ scrollView: UIScrollView) { 143 | if parent.currentPage == 0 && scrollView.contentOffset.x < scrollView.bounds.size.width || 144 | parent.currentPage == self.parent.controllers.count - 1 && scrollView.contentOffset.x > scrollView.bounds.size.width { 145 | scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0) 146 | } 147 | } 148 | 149 | private func disableVerticalBounce(_ scrollView: UIScrollView) { 150 | if parent.currentPage == 0 && scrollView.contentOffset.y < scrollView.bounds.size.height || 151 | parent.currentPage == self.parent.controllers.count - 1 && scrollView.contentOffset.y > scrollView.bounds.size.height { 152 | scrollView.contentOffset = CGPoint(x: 0, y: scrollView.bounds.size.height) 153 | } 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 |

4 |

5 | Getting Started | 6 | Customization | 7 | Installation 8 |

9 |

10 | CI 11 | Platforms 12 | 13 | License: MIT 14 |

15 | 16 |
17 | 18 |

19 | 20 | 21 | 22 | 23 | 24 |

25 | 26 | ## Getting Started 27 | 28 | ### Basic usage 29 | 30 | Using Pages is as easy as: 31 | 32 | ```swift 33 | 34 | import Pages 35 | 36 | struct WelcomeView: View { 37 | 38 | @State var index: Int = 0 39 | 40 | var body: some View { 41 | Pages(currentPage: $index) { 42 | Text("Welcome! This is Page 1") 43 | Text("This is Page 2") 44 | Text("...and this is Page 3") 45 | Circle() // The 4th page is a Circle 46 | } 47 | } 48 | } 49 | ``` 50 | 51 | One can also use Pages with dynamic content: 52 | 53 | ```swift 54 | 55 | import Pages 56 | 57 | struct Car { 58 | var model: String 59 | } 60 | 61 | struct CarsView: View { 62 | let cars = [Car(model: "Ford"), Car(model: "Ferrari")] 63 | @State var index: Int = 0 64 | 65 | var body: some View { 66 | ModelPages(cars, currentPage: $index) { pageIndex, car in 67 | Text("The \(pageIndex) car is a \(car.model)") 68 | .padding(50) 69 | .foregroundColor(.white) 70 | .background(Color.blue) 71 | .cornerRadius(10) 72 | } 73 | } 74 | } 75 | ``` 76 | 77 | ### How it works 78 | 79 | `Pages` uses a [function builder](https://github.com/apple/swift-evolution/blob/9992cf3c11c2d5e0ea20bee98657d93902d5b174/proposals/XXXX-function-builders.md) to accomplish a SwiftUI 80 | feel while using a `UIPageViewController` under the hood. As in `VStack` or `HStack`, the current limit of pages to 81 | add in a static way using the `Pages` view is 10. If more are needed use a `ModelPages` instead. The `Pages` view will take up all the available space it is given. 82 | 83 | > Note: The `Pages` view needs more than one page. Otherwise the compiler treats what's inside `Pages` as [a closure](https://stackoverflow.com/questions/58409839/function-builder-not-working-when-only-one-value). 84 | 85 | ## Customization 86 | 87 | The following aspects of `Pages` can be customized: 88 | 89 | - `navigationOrientation`: Whether to paginate horizontally or vertically. Default is `.horizontal`. 90 | 91 | ```swift 92 | Pages(navigationOrientation: .vertical) { 93 | Text("Page 1") 94 | Text("Page 2") 95 | } 96 | ``` 97 | 98 | - `transitionStyle`: Whether to perform a page curl or a scroll effect on page turn. The first two examples in the GIFs above use a scroll effect, and the last one uses page curl. Default is `.scroll`. 99 | 100 | ```swift 101 | Pages( 102 | navigationOrientation: .vertical, 103 | transitionStyle: .pageCurl 104 | ) { 105 | Text("Page 1") 106 | Text("Page 2") 107 | } 108 | ``` 109 | 110 | - `bounce`: Whether to perform a bounce effect when the user tries to scroll past the number of pages. Default is `true`. 111 | 112 | ```swift 113 | Pages( 114 | navigationOrientation: .vertical, 115 | transitionStyle: .pageCurl, 116 | bounce: false 117 | ) { 118 | Text("Page 1") 119 | Text("Page 2") 120 | } 121 | ``` 122 | 123 | - `wrap`: Whether to wrap the pages once a user tries to go to the next page after the last page. Similarly whether 124 | to go to the last page when the user scrolls to the previous page of the first page. Default is `false`. 125 | 126 | ```swift 127 | Pages( 128 | navigationOrientation: .vertical, 129 | transitionStyle: .pageCurl, 130 | bounce: false, 131 | wrap: true 132 | ) { 133 | Text("Page 1") 134 | Text("Page 2") 135 | } 136 | ``` 137 | 138 | - `hasControl`: Whether to display a page control or not. Default is `true`. 139 | 140 | ```swift 141 | Pages( 142 | navigationOrientation: .vertical, 143 | transitionStyle: .pageCurl, 144 | bounce: false, 145 | wrap: true, 146 | hasControl: false 147 | ) { 148 | Text("Page 1") 149 | Text("Page 2") 150 | } 151 | ``` 152 | 153 | - `control`: A user-defined control if one wants to tune it. If this field is not provided and `hasControl` is `true` then 154 | the classical iOS page control will be used. Note `control` must conform to `UIPageControl`. 155 | 156 | ```swift 157 | Pages( 158 | navigationOrientation: .vertical, 159 | transitionStyle: .pageCurl, 160 | bounce: false, 161 | wrap: true, 162 | control: MyPageControl() 163 | ) { 164 | Text("Page 1") 165 | Text("Page 2") 166 | } 167 | ``` 168 | 169 | - `controlAlignment`: Where to put the page control inside `Pages`. Default is `.bottom`. 170 | 171 | ```swift 172 | Pages( 173 | navigationOrientation: .vertical, 174 | transitionStyle: .pageCurl, 175 | bounce: false, 176 | wrap: true, 177 | controlAlignment: .topLeading 178 | ) { 179 | Text("Page 1") 180 | Text("Page 2") 181 | } 182 | ``` 183 | 184 | ## FAQ 185 | 186 | - How do I position my view to the left (`.leading`) or to the bottom right (`.bottomTrailing`)? 187 | 188 | - For example, if we want to position our `Text` view on the bottom trailing corner, we can use a `GeometryReader` to fill the available space: 189 | ```swift 190 | Pages(currentPage: $index) { 191 | GeometryReader { geometry in 192 | Text("Page 1") 193 | .frame(width: geometry.size.width, 194 | height: geometry.size.height, 195 | alignment: .bottomTrailing) 196 | } 197 | .background(Color.blue) 198 | GeometryReader { geometry in 199 | Text("Page 2") 200 | }.background(Color.red) 201 | } 202 | ``` 203 | Or the `Spacer` trick: 204 | ```swift 205 | Pages(currentPage: $index) { 206 | VStack { 207 | Spacer() 208 | HStack { 209 | Spacer() 210 | Text("Page 1") 211 | } 212 | } 213 | .background(Color.blue) 214 | GeometryReader { geometry in 215 | Text("Page 2") 216 | }.background(Color.red) 217 | } 218 | ``` 219 | 220 | ## Demos 221 | 222 | All of the demos shown on the GIF can be checked out on the [demo repo](https://github.com/nachonavarro/PagesDemo). 223 | 224 | ## Installation 225 | 226 | Pages is available using the [Swift Package Manager](https://swift.org/package-manager/): 227 | 228 | Using Xcode 11, go to `File -> Swift Packages -> Add Package Dependency` and enter https://github.com/nachonavarro/Pages 229 | 230 | ## Running the tests 231 | 232 | Once you select an iPhone destination on Xcode, press `⌘U` to run the tests. Alternatively run `xcodebuild test -destination 'name=iPhone 11' -scheme 'Pages'` on the terminal. 233 | 234 | ## Requirements 235 | 236 | - iOS 13.0+ 237 | - Xcode 11.0+ 238 | 239 | ## TODOs 240 | 241 | - Add unit and UI tests. 242 | - Improve function builder to include conditional clauses. 243 | - Merge `ModelPages` and `Pages` into one common view? 244 | 245 | ## Contributing 246 | 247 | Feel free to contribute to `Pages`! 248 | 249 | 1. Fork `Pages` 250 | 2. Create your feature branch with your changes 251 | 3. Create pull request 252 | 253 | ## License 254 | 255 | `Pages` is available under the MIT license. See the [LICENSE](https://github.com/nachonavarro/Pages/blob/master/LICENSE) for more info. 256 | --------------------------------------------------------------------------------