All Projects → fredyshox → Pageview

fredyshox / Pageview

Licence: mit
SwiftUI view enabling navigation between pages of content, imitating the behaviour of UIPageViewController for iOS and watchOS

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Pageview

Communicator
Communication between iOS and watchOS apps just got a whole lot better.
Stars: ✭ 94 (-17.54%)
Mutual labels:  watchos
Sniffer
Networking activity logger for Swift
Stars: ✭ 108 (-5.26%)
Mutual labels:  watchos
Articles Zh Hans
Articles for NSHipster.cn
Stars: ✭ 113 (-0.88%)
Mutual labels:  watchos
Egocache
Fast Caching for Objective-C (iPhone & Mac Compatible)
Stars: ✭ 1,339 (+1074.56%)
Mutual labels:  watchos
Awesome Rubymotion
A collection of awesome RubyMotion example apps, libraries, tools, frameworks, software and resources
Stars: ✭ 103 (-9.65%)
Mutual labels:  watchos
Swift Sdk
LeanCloud Swift SDK
Stars: ✭ 110 (-3.51%)
Mutual labels:  watchos
Swiftyattributes
A Swifty API for attributed strings
Stars: ✭ 1,303 (+1042.98%)
Mutual labels:  watchos
Mapboxgeocoder.swift
Address search and reverse geocoding in Swift or Objective-C on iOS, macOS, tvOS, and watchOS
Stars: ✭ 115 (+0.88%)
Mutual labels:  watchos
Swifterswift
A handy collection of more than 500 native Swift extensions to boost your productivity.
Stars: ✭ 10,706 (+9291.23%)
Mutual labels:  watchos
Ios Samples
Xamarin.iOS sample apps
Stars: ✭ 1,501 (+1216.67%)
Mutual labels:  watchos
Predicateflow
Write amazing, strong-typed and easy-to-read NSPredicate.
Stars: ✭ 98 (-14.04%)
Mutual labels:  watchos
Sdwebimagewebpcoder
A WebP coder plugin for SDWebImage, use libwebp
Stars: ✭ 101 (-11.4%)
Mutual labels:  watchos
Diff
Simple diff library in pure Swift
Stars: ✭ 110 (-3.51%)
Mutual labels:  watchos
Circularcrownselector
watchOS UI for a circular selection menu
Stars: ✭ 95 (-16.67%)
Mutual labels:  watchos
Flutter Watchtips
Flutter App (Embedded Watch Kit app with iOS version)
Stars: ✭ 114 (+0%)
Mutual labels:  watchos
Mixpanel
Unofficial Swift Mixpanel client
Stars: ✭ 93 (-18.42%)
Mutual labels:  watchos
Conbini
Publishers, operators, and subscribers to supplement Combine.
Stars: ✭ 109 (-4.39%)
Mutual labels:  watchos
Swiftui Kit
A SwiftUI system components and interactions demo app
Stars: ✭ 1,733 (+1420.18%)
Mutual labels:  watchos
Mapbox Directions Swift
Traffic-aware directions and map matching in Swift on iOS, macOS, tvOS, watchOS, and Linux
Stars: ✭ 115 (+0.88%)
Mutual labels:  watchos
Randomkit
Random data generation in Swift
Stars: ✭ 1,458 (+1178.95%)
Mutual labels:  watchos

PageView

SwiftUI view enabling page-based navigation, imitating the behaviour of UIPageViewController in iOS.

watchOS screenshow

Why

SwiftUI doesn't have any kind of paging control component, with features similar to UIPageViewController from UIKit. While on iOS this could be solved by wrapping UIPageViewController into UIViewRepresentable, on watchOS horizontal/vertical paging functionality cannot be achieved without using storyboards, which forces developers into using multiple WKHostingControllers.

This package attempts to provide native SwiftUI component for navigation between pages of content.

Installation

Package requires iOS 13, watchOS 6 and Xcode 11.

Swift Package Manager

For Swift Package Manager add the following package to your Package.swift:

.package(url: "https://github.com/fredyshox/PageView.git", .upToNextMajor(from: "1.4.1")),

Carthage

Carthage is also supported, add following line to Cartfile:

github "fredyshox/PageView" ~> 1.4.1

Demo

Demo app for both iOS and watchOS is provided in Examples/ directory.

Usage

import PageView

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:

@State var pageIndex = 0

...

// horizontal axis
HPageView(selectedPage: $pageIndex) {
    SomeCustomView()
    AnotherCustomView()
    AnotherCustomView()
}

// vertical axis
VPageView(selectedPage: $pageIndex) {
    SomeCustomView()
    AnotherCustomView()
    AnotherCustomView()
}

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

Selected page binding

Displayed page can be programmatically controled using Binding. For example, you can change it, with animation effect using:

withAnimation {
  // page index is some State property, which binding was passes into PageView
  self.pageIndex = 2
}

Page switch threshold

You can also control minimum distance that needs to be scrolled to switch page, expressed in fraction of page dimension (width or height, depending on axis). This parameter is called pageSwitchThreshold, and must be in range from 0.0 to 1.0.

For iOS the default value is set to 0.3, while on watchOS 0.5.

Theme

Styling of page control component can be customized by passing PageControlTheme. Customizable properties:

  • backgroundColor
  • dotActiveColor: active page dot color
  • dotInactiveColor: inactive page dot color
  • dotSize: size of page dot
  • spacing: spacing between dots
  • padding: padding of page control
  • xOffset: page control x-axis offset, used only in vertical mode
  • yOffset: page control y-axis offset, used only in horizontal mode
  • alignment: alignment of page control component (default: bottom-center in horizontal mode, center-leading in vertical mode)
let theme = PageControlTheme(
    backgroundColor: .white,
    dotActiveColor: .black,
    dotInactiveColor: .gray,
    dotSize: 10.0,
    spacing: 12.0,
    padding: 5.0,
  	xOffset: 8.0,
    yOffset: -8.0,
    alignment: Alignment(horizontal: .trailing, vertical: .top)
)
...
VPageView(theme: theme) {
    ...
}

There is also a built-in PageControlTheme.default style, mimicking UIPageControl appearance.

API

// Horizontal page view
public struct HPageView<Pages>: View where Pages: View {
    public init(
        selectedPage: Binding<Int>,
        pageSwitchThreshold: CGFloat = .defaultSwitchThreshold,
        theme: PageControlTheme = .default,
        @PageViewBuilder builder: () -> PageContainer<Pages>
    )
}

// Vertical page view
public struct VPageView<Pages>: View where Pages: View {
    public init(
        selectedPage: Binding<Int>,
        pageSwitchThreshold: CGFloat = .defaultSwitchThreshold,
        theme: PageControlTheme = .default,
        @PageViewBuilder builder: () -> PageContainer<Pages>
    )
}

public struct PageControlTheme {
    public var backgroundColor: Color
    public var dotActiveColor: Color
    public var dotInactiveColor: Color
    public var dotSize: CGFloat
    public var spacing: CGFloat
    public var padding: CGFloat
    public var xOffset: CGFloat
    public var yOffset: CGFloat
    public var alignment: Alignment?
}

Screenshots

iOS example

HPageView on watchOS

VPageView on watchOS

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].