/SwiftUIPager

Primary LanguageSwiftMIT LicenseMIT

SwiftUIPager

Swift Package Manager compatible Cocoapods Carthage compatible CocoaPods platforms License: MIT

SwiftUIPager provides a Pager component built with SwiftUI native components. Pager is a view that renders a scrollable container to display a handful of pages. These pages are recycled on scroll, so you don't have to worry about memory issues.

Create vertical or horizontal pagers, align the cards, change the direction of the scroll, animate the pagintation... Pager lets you do anything you want.

Example of usage

Requirements

  • iOS 13.0+
  • macOS 10.15+
  • watchOS 6.0+
  • Swift 5.1+

Installation

CocoaPods

pod 'SwiftUIPager'

Swift Package Manager

Go to XCode:

Carthage

github "fermoya/SwiftUIPager"

Usage

Initialization

Creating a Pager is very simple. You just need to pass:

  • Array of items
  • KeyPath to an identifier.
  • ViewBuilder factory method to create each page
Pager(page: $page
      data: items,
      id: \.identifier,
      content: { item in
           // create a page based on the data passed
           self.pageView(item)
 })

UI customization

Pager is easily customizable through a number of view-modifier functions. You can change the orientation, the direction of the scroll, the alignment, the space between items or the page aspect ratio, among others:

Pager(...)
     .itemSpacing(10)
     .padding(8)
     .itemAspectRatio(0.6)

itemAspectRatio will change the look of the page. Use a value lower than 1 to make the page look like a card:

itemAspectRatio lower than 1

whereas a value greater than one will make it look like a box:

itemAspectRatio greater than 1

Add a position alignment to itemAspectRatio to specify the alignment inside Pager:

Pager(...)
     .itemSpacing(10)
     .padding(8)
     .itemAspectRatio(1.5, alignment: .end)

Pages positioned at the start of the horizontal pager

By default, Pager will create a horizontal container. Use vertical to create a vertical pager:

Pager(...)
    .vertical()

Vertical pager

You can customize the alignment and the direction of the scroll. For instance, you can have a horizontal Pager that scrolls right-to-left that it's aligned at the start of the scroll:

Pager(...)
    .itemSpacing(10)
    .alignment(.start)
    .horizontal(.rightToLeft)
    .itemAspectRatio(0.6)

Pages aligned to the start of the pager

Animations

Use interactive add a scale animation effect to those pages that are unfocused, that is, those elements whose index is different from pageIndex:

Pager(...)
    .interactive(0.8)

Interactive pager

You can also use rotation3D to add a rotation effect to your pages:

Pager(...)
    .itemSpacing(10)
    .rotation3D()

Rotation 3D

Events

Use onPageChanged to react to any change on the page index:

Pager(...)
     .onPageChanged({ (newIndex) in
         // do something
     })

Sample projects

You can use Pager to implement cool effects as in iPod

Cool sample with Pager

For more information, please check the sample app.

If you have any issues or feedback, please open an issue or reach out to me at fmdr.ct@gmail.com.
Please feel free to collaborate and make this framework better.

License

SwiftUIPager is available under the MIT license. See the LICENSE file for more info.