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

时间:2016-12-03 16:26:46

标签: ios swift xcode uiviewcontroller uipagecontrol

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

这是代码 -

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
}
}

2 个答案:

答案 0 :(得分:3)

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

因此,在显示您要求UserDefaults.standard.set( true, forKey: "firstTimeOpened") 中的值的页面之前,如果值不存在则设置它。

Home View Controller

<强> EDITED

我认为您的FirstVCFirstVC所以试试这个:

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") }

PageView

之后,您可以关闭{{1}}并返回主页,如果您重新启动应用,则不会显示PageView。

答案 1 :(得分:0)

在页面控制器的viewDidiLoad方法中,只调用(推送/呈现)主屏幕控制器,没有任何动画效果。

或者将主屏幕设置为带有导航控制器的rootviewController

像这样:     class func setHomeScreen(){         var mainView:UIStoryboard!         mainView = UIStoryboard(名称:&#34; HomeScreen&#34 ;, bundle:nil)         让viewcontroller:UINavigationController = mainView.instantiateViewControllerWithIdentifier(&#34; HomeNavigationViewController&#34;)as! HomeNavigationViewController         AppDelegate.getAppDelegate()。window!.rootViewController = viewcontroller

}

enter image description here