如何仅在首次启动 IOS Swift 时显示页面控制器

Jim*_*Joe 1 xcode uiviewcontroller uipagecontrol ios swift

我只想在用户第一次打开应用程序时显示页面控制器,之后当他再次打开时,它会跳过页面控制器并直接移动到主屏幕。

这是代码 -

import UIKit

class PageVC: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

lazy var VCArr: [UIViewController] = {
    return [self.VCInstance(name: "FirstVC"),
            self.VCInstance(name: "SecondVC"),
            self.VCInstance(name: "ThirdVC")]
}()

private func VCInstance(name: String) -> UIViewController {
    return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: name)
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    self.dataSource = self
    self.delegate = self
    if let firstVC = VCArr.first {
        setViewControllers([firstVC], direction: .forward, animated: true, completion: nil)
    }
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    for view in self.view.subviews {
        if view is UIScrollView {
            view.frame = UIScreen.main.bounds
        } else if view is UIPageControl {
            view.backgroundColor = UIColor.clear
        }
    }
}


public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
    guard let viewControllerIndex = VCArr.index(of: viewController) else {
        return nil
    }

    let previousIndex = viewControllerIndex - 1

    guard previousIndex >= 0 else {
        return VCArr.last
    }

    guard VCArr.count > previousIndex else {
        return nil
    }

    return VCArr[previousIndex]
}

public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
    guard let viewControllerIndex = VCArr.index(of: viewController) else {
        return nil
    }

    let nextIndex = viewControllerIndex + 1

    guard nextIndex < VCArr.count else {
        return VCArr.first
    }

    guard VCArr.count > nextIndex else {
        return nil
    }

    return VCArr[nextIndex]
}

public func presentationCount(for pageViewController: UIPageViewController) -> Int {
    return VCArr.count
}


public func presentationIndex(for pageViewController: UIPageViewController) -> Int {
    guard let firstViewController = viewControllers?.first,
        let firstViewControllerIndex = VCArr.index(of: firstViewController) else {
            return 0
    }

    return firstViewControllerIndex
}
}
Run Code Online (Sandbox Code Playgroud)

San*_*lez 5

您可以使用UserDefaults设置一个Bool值来定义是否是第一次打开应用程序。

因此,在显示页面之前,您要求输入 中的值UserDefaults,如果该值不存在,则设置它。

UserDefaults.standard.set( true, forKey: "firstTimeOpened")
Run Code Online (Sandbox Code Playgroud)

已编辑

我想你Home View Controller是FirstVC这样试试这个:

在你FirstVC的ViewDidAppear功能上:

let firstTime = UserDefaults.standard().object(ForKey: "first_time") as? Bool // Here you look if the Bool value exists, if not it means that is the first time the app is opened

// Show the intro collectionView
if firstTime == nil {
    let view = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "YourPageVC") // Instatiates your pageView
    show(view, sender: nil)
    UserDefaults.standard().set(false, forKey: "first_time")
}
Run Code Online (Sandbox Code Playgroud)

之后,您可以关闭PageView并返回主页,如果您重新启动应用程序,则不会出现 PageView。