Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for default UIPageViewController animations #145

Merged
merged 6 commits into from
Feb 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ The following styles are available:
- `.moveIn`
- `.reveal`

*Note: By default this is set to `nil`, which uses the standard animation provided by `UIPageViewController`.*

### Auto Scrolling
`PageboyAutoScroller` is available to set up timer based automatic scrolling of the `PageboyViewController`:

Expand Down
24 changes: 0 additions & 24 deletions Sources/Pageboy.podspec

This file was deleted.

20 changes: 13 additions & 7 deletions Sources/Pageboy/Extensions/PageboyViewController+Management.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,27 @@ internal extension PageboyViewController {

targetIndex = toIndex
isUpdatingViewControllers = true
performTransition(from: fromIndex,
to: toIndex,
with: direction,
animated: animated,
completion: completion ?? { _ in })

let isUsingCustomTransition = transition != nil
if isUsingCustomTransition {
performTransition(from: fromIndex,
to: toIndex,
with: direction,
animated: animated,
completion: completion ?? { _ in })
}

// if not using a custom transition then animate using UIPageViewController mechanism
let animateUpdate = animated ? !isUsingCustomTransition : false
let updateBlock = {
pageViewController.setViewControllers(viewControllers,
direction: direction.pageViewControllerNavDirection,
animated: false,
animated: animateUpdate,
completion:
{ (finished) in
self.isUpdatingViewControllers = false

if !animated {
if !animated || !isUsingCustomTransition {
completion?(finished)
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public extension PageboyViewController {
public let duration: TimeInterval

/// Default transition (Push, 0.3 second duration).
@available(*, obsoleted: 2.4.0, message:"To use the default transition, set PageboyViewController.transition to `nil`")
public static var `default`: Transition {
return Transition(style: .push, duration: 0.3)
}
Expand Down Expand Up @@ -75,7 +76,7 @@ internal extension PageboyViewController {
// MARK: Animation

@objc func displayLinkDidTick() {
self.activeTransition?.tick()
self.activeTransitionOperation?.tick()
}

/// Perform a transition to a new page index.
Expand All @@ -91,11 +92,14 @@ internal extension PageboyViewController {
with direction: NavigationDirection,
animated: Bool,
completion: @escaping TransitionOperation.Completion) {
guard animated == true, self.activeTransition == nil else {
return
guard let transition = self.transition,
animated == true,
self.activeTransitionOperation == nil else {
completion(false)
return
}
guard let scrollView = self.pageViewController?.scrollView else {
return
fatalError("Can't find UIPageViewController scroll view")
}

prepareForTransition()
Expand All @@ -112,13 +116,13 @@ internal extension PageboyViewController {
direction: direction,
semanticDirection: semanticDirection,
orientation: self.navigationOrientation)
self.activeTransition = TransitionOperation(for: self.transition,
self.activeTransitionOperation = TransitionOperation(for: transition,
action: action,
delegate: self)
self.transitionDisplayLink?.isPaused = false

// start transition
self.activeTransition?.start(on: scrollView.layer,
self.activeTransitionOperation?.start(on: scrollView.layer,
completion: completion)
}
}
Expand All @@ -128,7 +132,7 @@ extension PageboyViewController: TransitionOperationDelegate {
func transitionOperation(_ operation: TransitionOperation,
didFinish finished: Bool) {
self.transitionDisplayLink?.isPaused = true
self.activeTransition = nil
self.activeTransitionOperation = nil

clearUpAfterTransition()
}
Expand Down
44 changes: 24 additions & 20 deletions Sources/Pageboy/PageboyViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,13 @@ open class PageboyViewController: UIViewController {
/// Whether the view controllers in the page view controller are currently updating.
internal var isUpdatingViewControllers: Bool = false

/// The transition to use when animating scrolls between pages.
public var transition = Transition.default
/// Custom transition to use when animating scrolls between pages.
/// Setting this to `nil` will revert to using the standard UIPageViewController animation.
public var transition: Transition?
/// The display link for transitioning.
internal var transitionDisplayLink: CADisplayLink?
/// The active transition operation.
internal var activeTransition: TransitionOperation?
internal var activeTransitionOperation: TransitionOperation?

/// The number of view controllers in the page view controller.
internal var viewControllerCount: Int?
Expand All @@ -180,23 +181,7 @@ open class PageboyViewController: UIViewController {
guard let currentIndex = self.currentIndex else {
return
}

#if os(iOS)
UIView.animate(withDuration: 0.3) {
self.setNeedsStatusBarAppearanceUpdate()
}
#endif

// ensure position keeps in sync
self.currentPosition = CGPoint(x: self.navigationOrientation == .horizontal ? CGFloat(currentIndex) : 0.0,
y: self.navigationOrientation == .vertical ? CGFloat(currentIndex) : 0.0)
let direction = NavigationDirection.forPosition(CGFloat(currentIndex),
previous: CGFloat(oldValue ?? currentIndex))
self.delegate?.pageboyViewController(self,
didScrollToPageAt: currentIndex,
direction: direction,
animated: self.isScrollingAnimated)

update(forNew: currentIndex, from: oldValue)
}
}
/// The relative page position that the page view controller is currently at.
Expand Down Expand Up @@ -356,4 +341,23 @@ public extension PageboyViewController {

return direction
}

private func update(forNew currentIndex: PageIndex, from oldIndex: PageIndex?) {

#if os(iOS)
UIView.animate(withDuration: 0.3) {
self.setNeedsStatusBarAppearanceUpdate()
}
#endif

// ensure position keeps in sync
self.currentPosition = CGPoint(x: self.navigationOrientation == .horizontal ? CGFloat(currentIndex) : 0.0,
y: self.navigationOrientation == .vertical ? CGFloat(currentIndex) : 0.0)
let direction = NavigationDirection.forPosition(CGFloat(currentIndex),
previous: CGFloat(oldIndex ?? currentIndex))
self.delegate?.pageboyViewController(self,
didScrollToPageAt: currentIndex,
direction: direction,
animated: self.isScrollingAnimated)
}
}