Skip to content

Commit

Permalink
Add Swift 4 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
msaps committed Nov 13, 2018
1 parent 1893e0e commit 132460d
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Sources/Pageboy.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@
PRODUCT_NAME = Pageboy;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -704,7 +704,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.uias.Pageboy;
PRODUCT_NAME = Pageboy;
SKIP_INSTALL = YES;
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand Down
32 changes: 32 additions & 0 deletions Sources/Pageboy/PageboyViewController+Management.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,27 @@ internal extension PageboyViewController {
pageViewController.dataSource = self
self.pageViewController = pageViewController

#if swift(>=4.2)
addChild(pageViewController)
#else
addChildViewController(pageViewController)
#endif
if let existingZIndex = existingZIndex {
view.insertSubview(pageViewController.view, at: existingZIndex)
} else {
view.addSubview(pageViewController.view)
#if swift(>=4.2)
view.sendSubviewToBack(pageViewController.view)
#else
view.sendSubview(toBack: pageViewController.view)
#endif
}
pageViewController.view.pinToSuperviewEdges()
#if swift(>=4.2)
pageViewController.didMove(toParent: self)
#else
pageViewController.didMove(toParentViewController: self)
#endif

pageViewController.scrollView?.delegate = self
pageViewController.view.backgroundColor = .clear
Expand All @@ -185,7 +197,11 @@ internal extension PageboyViewController {

private func destroyCurrentPageViewController() {
pageViewController?.view.removeFromSuperview()
#if swift(>=4.2)
pageViewController?.removeFromParent()
#else
pageViewController?.removeFromParentViewController()
#endif
pageViewController = nil
}

Expand All @@ -197,6 +213,7 @@ internal extension PageboyViewController {
setUpPageViewController(reloadViewControllers: false)
}

#if swift(>=4.2)
/// The options to be passed to a UIPageViewController instance.
internal var pageViewControllerOptions: [UIPageViewController.OptionsKey: Any]? {
var options = [UIPageViewController.OptionsKey: Any]()
Expand All @@ -210,6 +227,21 @@ internal extension PageboyViewController {
}
return options
}
#else
/// The options to be passed to a UIPageViewController instance.
internal var pageViewControllerOptions: [String: Any]? {
var options = [String: Any]()

if interPageSpacing > 0.0 {
options[UIPageViewControllerOptionInterPageSpacingKey] = interPageSpacing
}

guard options.count > 0 else {
return nil
}
return options
}
#endif
}

// MARK: - UIPageViewControllerDataSource, PageboyViewControllerDataSource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ internal extension PageboyViewController {

let displayLink = CADisplayLink(target: self, selector: #selector(displayLinkDidTick))
displayLink.isPaused = true
#if swift(>=4.2)
displayLink.add(to: .main, forMode: .common)
#else
displayLink.add(to: .main, forMode: .commonModes)
#endif
transitionDisplayLink = displayLink
}

Expand Down Expand Up @@ -156,6 +160,10 @@ internal extension PageboyViewController.Transition {

func configure(transition: inout CATransition) {
transition.duration = duration
#if swift(>=4.2)
transition.type = CATransitionType(rawValue: style.rawValue)
#else
transition.type = style.rawValue
#endif
}
}
26 changes: 26 additions & 0 deletions Sources/Pageboy/Transitioning/TransitionOperation+Action.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ internal extension TransitionOperation {

internal extension TransitionOperation.Action {

#if swift(>=4.2)
/// Animation sub-type for the action.
var transitionSubType: CATransitionSubtype {
switch orientation {
Expand All @@ -53,4 +54,29 @@ internal extension TransitionOperation.Action {
}
}
}
#else
/// Animation sub-type for the action.
var transitionSubType: String {
switch orientation {

case .horizontal:
switch semanticDirection {

case .reverse:
return kCATransitionFromLeft
default:
return kCATransitionFromRight
}

case .vertical:
switch semanticDirection {

case .reverse:
return kCATransitionFromBottom
default:
return kCATransitionFromTop
}
}
}
#endif
}
4 changes: 4 additions & 0 deletions Sources/Pageboy/Transitioning/TransitionOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ internal class TransitionOperation: NSObject, CAAnimationDelegate {
animation.endProgress = 1.0
transition.configure(transition: &animation)
animation.subtype = action.transitionSubType
#if swift(>=4.2)
animation.fillMode = .backwards
#else
animation.fillMode = kCAFillModeBackwards
#endif
self.animation = animation

super.init()
Expand Down

0 comments on commit 132460d

Please sign in to comment.