Skip to content

Commit

Permalink
Merge pull request #12 from DeluxeAlonso/development
Browse files Browse the repository at this point in the history
Development to master for 1.3.1 version
  • Loading branch information
DeluxeAlonso authored Jul 11, 2021
2 parents 1c021e4 + bbfe02c commit 5a84595
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 33 deletions.
2 changes: 1 addition & 1 deletion DLAutoSlidePageViewController.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'DLAutoSlidePageViewController'
s.version = '1.2.1'
s.version = '1.3.1'
s.summary = 'An auto slide PageViewController.'

s.description = <<-DESC
Expand Down
19 changes: 11 additions & 8 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ let pageViewController = DLAutoSlidePageViewController(pages: pages, configurati
| timeInterval | TimeInterval | Time interval to be used for each page automatic transition. |
| transitionStyle | UIPageViewController.TransitionStyle | Styles for the page-turn transition. |
| navigationOrientation | UIPageViewController.NavigationOrientation | Orientations for page-turn transitions. |
| navigationDirection | UIPageViewController.NavigationDirection | Directions for page-turn transitions. |
| interPageSpacing | Float | Space between pages. |
| spineLocation | UIPageViewController.SpineLocation | Locations for the spine. Only valid if the transition style is UIPageViewController.TransitionStyle.pageCurl. |
| hidePageControl | Bool | Decides if page control is going to be shown or not. |
Expand Down
4 changes: 4 additions & 0 deletions Sources/AutoSlideConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public protocol AutoSlideConfiguration {
/// Orientations for page-turn transitions.
var navigationOrientation: UIPageViewController.NavigationOrientation { get }

/// Directions for page-turn transitions.
var navigationDirection: UIPageViewController.NavigationDirection { get }

/// Space between pages.
var interPageSpacing: Float { get }

Expand Down Expand Up @@ -45,6 +48,7 @@ public extension AutoSlideConfiguration {
var timeInterval: TimeInterval { 3.0 }
var transitionStyle: UIPageViewController.TransitionStyle { .scroll }
var navigationOrientation: UIPageViewController.NavigationOrientation { .horizontal }
var navigationDirection: UIPageViewController.NavigationDirection { .forward }
var interPageSpacing: Float { 0.0 }
var spineLocation: UIPageViewController.SpineLocation { .none }
var hidePageControl: Bool { false }
Expand Down
25 changes: 25 additions & 0 deletions Sources/AutoSlideHelper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// AutoSlideHelper.swift
// DLAutoSlidePageViewController
//
// Created by Alonso on 11/07/21.
//

import UIKit

final class AutoSlideHelper {

class func pageIndex(for currentPageIndex: Int,
totalPageCount: Int,
direction: UIPageViewController.NavigationDirection) -> Int {
switch direction {
case .reverse:
return currentPageIndex > 0 ? currentPageIndex - 1 : totalPageCount - 1
case .forward:
fallthrough
@unknown default:
return currentPageIndex < totalPageCount - 1 ? currentPageIndex + 1 : 0
}
}

}
51 changes: 27 additions & 24 deletions Sources/DLAutoSlidePageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ import UIKit

open class DLAutoSlidePageViewController: UIPageViewController {

fileprivate(set) var pages: [UIViewController] = []
private var pages: [UIViewController] = []

fileprivate var currentPageIndex: Int = 0
fileprivate var nextPageIndex: Int = 0
fileprivate var timer: Timer?
fileprivate var timeInterval: TimeInterval = 0.0
fileprivate var transitionInProgress: Bool = false
fileprivate var shouldHidePageControl: Bool = false
private var currentPageIndex: Int = 0
private var nextPageIndex: Int = 0
private var timer: Timer?

private var timeInterval: TimeInterval = 0.0
private var shouldHidePageControl: Bool = false
private var navigationDirection: UIPageViewController.NavigationDirection = .forward

private var transitionInProgress: Bool = false

// MARK: - Computed properties

Expand All @@ -34,8 +37,10 @@ open class DLAutoSlidePageViewController: UIPageViewController {
options: [UIPageViewController.OptionsKey.interPageSpacing: configuration.interPageSpacing,
UIPageViewController.OptionsKey.spineLocation: configuration.spineLocation])
self.pages = pages

self.timeInterval = configuration.timeInterval
self.shouldHidePageControl = configuration.hidePageControl
self.navigationDirection = configuration.navigationDirection

setupPageView()
setupPageTimer(with: timeInterval)
Expand Down Expand Up @@ -77,30 +82,30 @@ open class DLAutoSlidePageViewController: UIPageViewController {

// MARK: - Private

fileprivate func setupObservers() {
private func setupObservers() {
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(movedToForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
}

fileprivate func setupPageView() {
private func setupPageView() {
guard let firstPage = pages.first else { return }
currentPageIndex = 0
setViewControllers([firstPage], direction: .forward, animated: true, completion: nil)
setViewControllers([firstPage], direction: navigationDirection, animated: true, completion: nil)
}

fileprivate func setupPageControl(with configuration: AutoSlideConfiguration) {
private func setupPageControl(with configuration: AutoSlideConfiguration) {
pageControl?.currentPageIndicatorTintColor = configuration.currentPageIndicatorTintColor
pageControl?.pageIndicatorTintColor = configuration.pageIndicatorTintColor
pageControl?.backgroundColor = configuration.pageControlBackgroundColor
}

fileprivate func viewControllerAtIndex(_ index: Int) -> UIViewController {
private func viewControllerAtIndex(_ index: Int) -> UIViewController {
guard index < pages.count else { return UIViewController() }
currentPageIndex = index
return pages[index]
}

fileprivate func setupPageTimer(with timeInterval: TimeInterval) {
private func setupPageTimer(with timeInterval: TimeInterval) {
guard timeInterval != 0.0 else { return }
timer = Timer.scheduledTimer(timeInterval: timeInterval,
target: self,
Expand All @@ -109,35 +114,33 @@ open class DLAutoSlidePageViewController: UIPageViewController {
repeats: true)
}

fileprivate func stopTimer() {
private func stopTimer() {
guard let _ = timer else { return }
timer?.invalidate()
timer = nil
}

fileprivate func restartTimer() {
private func restartTimer() {
stopTimer()
setupPageTimer(with: timeInterval)
}

// MARK: - Selectors

@objc fileprivate func movedToForeground() {
@objc private func movedToForeground() {
transitionInProgress = false
restartTimer()
}

@objc fileprivate func changePage() {
if currentPageIndex < pages.count - 1 {
currentPageIndex += 1
} else {
currentPageIndex = 0
}
@objc private func changePage() {
currentPageIndex = AutoSlideHelper.pageIndex(for: currentPageIndex,
totalPageCount: pages.count,
direction: navigationDirection)
guard let viewController = viewControllerAtIndex(currentPageIndex) as UIViewController? else { return }
if !transitionInProgress {
transitionInProgress = true
setViewControllers([viewController], direction: .forward, animated: true, completion: { finished in
self.transitionInProgress = !finished
setViewControllers([viewController], direction: navigationDirection, animated: true, completion: { finished in
self.transitionInProgress = false
})
}
}
Expand Down
1 change: 1 addition & 0 deletions Sources/DefaultAutoSlideConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ final public class DefaultAutoSlideConfiguration: AutoSlideConfiguration {
public var timeInterval: TimeInterval = 3.0
public var transitionStyle: UIPageViewController.TransitionStyle = .scroll
public var navigationOrientation: UIPageViewController.NavigationOrientation = .horizontal
public var navigationDirection: UIPageViewController.NavigationDirection = .forward
public var interPageSpacing: Float = 0.0
public var spineLocation: UIPageViewController.SpineLocation = .none
public var hidePageControl: Bool = false
Expand Down

0 comments on commit 5a84595

Please sign in to comment.