我想仅在用户第一次打开应用程序时显示页面控制器,然后再次打开时,它会跳过页面控制器并直接移动到主屏幕。
这是代码 -
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
}
}
答案 0 :(得分:3)
您可以使用Bool
设置UserDefaults
值,该值定义应用程序是否第一次打开。
因此,在显示您要求UserDefaults.standard.set( true, forKey: "firstTimeOpened")
中的值的页面之前,如果值不存在则设置它。
Home View Controller
<强> EDITED 强>
我认为您的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")
}
:
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}