如何仅在第一次启动时显示页面控制器 IOS Swift
How can I display page controller only on the first launch 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
}
}
在页面控制器的viewDidiLoad方法中只调用(push/present)主屏幕控制器,没有任何动画效果。
或者将主屏幕设置为带导航控制器的 rootviewController
像这样:
class func setHomeScreen(){
var mainView: UIStoryboard!
mainView = UIStoryboard(name: "HomeScreen", bundle: nil)
让 viewcontroller : UINavigationController = mainView.instantiateViewControllerWithIdentifier("HomeNavigationViewController") 作为! HomeNavigationViewController
AppDelegate.getAppDelegate().window!.rootViewController = viewcontroller
}
您可以使用 UserDefaults
设置一个 Bool
值来定义应用程序是否是第一次打开。
因此,在显示页面之前,您需要 UserDefaults
中的值,如果该值不存在,请设置它。
UserDefaults.standard.set( true, forKey: "firstTimeOpened")
已编辑
我想你的 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")
}
之后您可以将您的 PageView
和 return 关闭到主页,如果您重新启动您的应用程序,PageView 将不会出现。
我只想在用户第一次打开应用程序时显示页面控制器,然后当他再次打开时,它会跳过页面控制器并直接移动到主屏幕。
这是代码 -
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
}
}
在页面控制器的viewDidiLoad方法中只调用(push/present)主屏幕控制器,没有任何动画效果。
或者将主屏幕设置为带导航控制器的 rootviewController
像这样: class func setHomeScreen(){ var mainView: UIStoryboard! mainView = UIStoryboard(name: "HomeScreen", bundle: nil) 让 viewcontroller : UINavigationController = mainView.instantiateViewControllerWithIdentifier("HomeNavigationViewController") 作为! HomeNavigationViewController AppDelegate.getAppDelegate().window!.rootViewController = viewcontroller
}
您可以使用 UserDefaults
设置一个 Bool
值来定义应用程序是否是第一次打开。
因此,在显示页面之前,您需要 UserDefaults
中的值,如果该值不存在,请设置它。
UserDefaults.standard.set( true, forKey: "firstTimeOpened")
已编辑
我想你的 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")
}
之后您可以将您的 PageView
和 return 关闭到主页,如果您重新启动您的应用程序,PageView 将不会出现。