我试图在人们面临同样问题的现有问题中寻找答案,但这些问题的解决方案是我已经解决的问题。这是我的代码:
AppDelegate.swift
import UIKit
@UIApplicationMain
final class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let onboardingCoordinator = OnboardingViewCoordinator()
let onboardingPageViewController = WarmPushPageViewController(coordinator: onboardingCoordinator)
self.window = UIWindow(frame: UIScreen.main.bounds)
guard let window = self.window else { return false }
window.rootViewController = onboardingPageViewController
window.makeKeyAndVisible()
return true
}
}
您的典型应用代表,尽量保持尽可能小。
Coordinator.swift
import UIKit
enum PageViewDirection {
case before
case after
}
typealias Index = Int
protocol PageViewCoordinator: class {
var currentIndex: Index { get set }
var viewControllers: [UIViewController] { get set }
func present(direction: PageViewDirection) -> UIViewController?
}
OnboardingViewCoordinator.swift
import UIKit
final class OnboardingViewCoordinator: PageViewCoordinator {
var currentIndex: Index
var viewControllers: [UIViewController]
init() {
self.viewControllers = [
OnboardingTaglineViewController(nibName: nil, bundle: nil),
OnboardingSourcesViewController(nibName: nil, bundle: nil)
]
self.currentIndex = 0
}
func present(direction: PageViewDirection) -> UIViewController? {
switch direction {
case .before:
if self.currentIndex == 0 {
return nil
}
self.currentIndex -= 1
let beforeVC = self.viewControllers[self.currentIndex]
return beforeVC
case .after:
if self.currentIndex == (self.viewControllers.count - 1) {
return nil
}
self.currentIndex += 1
let afterVC = self.viewControllers[self.currentIndex]
return afterVC
}
}
}
WarmPushPageViewController.swift
import UIKit
class WarmPushPageViewController: UIPageViewController {
var coordinator: PageViewCoordinator? = nil
convenience init(coordinator: PageViewCoordinator) {
self.init(transitionStyle: .scroll,
navigationOrientation: .horizontal,
options: nil)
self.setViewControllers([coordinator.viewControllers[coordinator.currentIndex]],
direction: .forward,
animated: true,
completion: nil)
self.coordinator = coordinator
}
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
self.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension WarmPushPageViewController: UIPageViewControllerDataSource {
func pageViewController(_ pageViewController: UIPageViewController,
viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let coordinator = self.coordinator else { return nil }
guard let beforeVC = coordinator.present(direction: .before) else { return nil }
return beforeVC
}
func pageViewController(_ pageViewController: UIPageViewController,
viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let coordinator = self.coordinator else { return nil }
guard let afterVC = coordinator.present(direction: .after) else { return nil }
return afterVC
}
}
提前致谢!
最佳, Kiren:)