主视图
主视图是指应用的内容主界面
添加viewDidAppear(_:animated)中通过withIdentifier实例化视图容器
通过present(UIViewController:animated:completion)显示视图
import UIKit
class ViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if UserDefaults.standard.bool(forKey: "hasViewedWalkthrough") {
return
}
if let pageViewController = storyboard?.instantiateViewController(withIdentifier: "WalkthroughController") as? WalkthroughPageViewController {
present(pageViewController, animated: true, completion: nil)
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
滑动视图
容器视图
-
创建自定义的类继承UIPageViewController并,实现UIPageViewControllerDataSource协议
定义每个视图显示的内容数组
-
实现UIPageViewControllerDataSource协议的两个方法当用户导航切换页面是都会被调用
指定和返回一个显示上一个或是下一个指定的content view controllerpageViewController(_:viewControllerBefore:)
pageViewController(_:viewControllerAfter:)
在viewDidLoad中将dataSource设置为self
在view中调用setViewControllers方法,在参数中设置要显示的内容视图
-
定义一个辅助方法用于返回内容视图
避免数组索引越界
通过storyboard?.instantiateViewController(withIdentifier:)实例化一个视图,并设置相关的视图内容,如UILabel等。
返回内容视图
-
创建Page的指示器的两种方式
-
默认的指示器
-
需要实现UIPageViewControllerDataSource协议的两个可选方法
presentationCount(for:)返回页面总数
presentationIndex(for:)返回当前选中的页面指示器索引
-
-
自定义的指示器
从组件库中拖拽出一个 Page controller组件到视图,并放到合适的位置添加好约束
在内容视图类定义一个@IBOutlet var pagecontrol连接到视图中的Page Control组件,并设置总数和当前所在的索引
在viewDidLoad中赋值 pageControl.currentPage = index
-
import UIKit
class WalkthroughPageViewController: UIPageViewController, UIPageViewControllerDataSource {
var pageHeadings = ["Personalize", "Locate", "Discover"]
var pageImages = ["foodpin-intro-1", "foodpin-intro-2", "foodpin-intro-3"]
var pageContent = ["Pin your favorite restaurants and create your own food guide", "Search and locate your favourite restaurant on Maps", "Find restaurants pinned by your friends and other foodies around the world"]
override func viewDidLoad() {
super.viewDidLoad()
dataSource = self
if let startingViewController = contentViewController(at: 0) {
setViewControllers([startingViewController], direction: .forward, animated: true, completion: nil)
} else {
print("nil")
}
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
var index = (viewController as! WalkthroughContentViewController).index
index -= 1
return contentViewController(at: index)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
var index = (viewController as! WalkthroughContentViewController).index
index += 1
return contentViewController(at: index)
}
func contentViewController(at index: Int) -> WalkthroughContentViewController? {
if index < 0 || index >= pageHeadings.count {
return nil
}
if let pageContentViewController = storyboard?.instantiateViewController(withIdentifier: "WalkthroughContentViewController") as? WalkthroughContentViewController {
pageContentViewController.heading = pageHeadings[index]
pageContentViewController.content = pageContent[index]
pageContentViewController.imageFile = pageImages[index]
pageContentViewController.index = index
return pageContentViewController
}
return nil
}
func forward(index: Int) {
if let nextViewController = contentViewController(at: index + 1) {
setViewControllers([nextViewController], direction: .forward, animated: true, completion: nil)
}
}
}
内容视图
是一个可重用的视图,继承正常的UIViewController
在这里定义视图上的内容,如UILabel、UIImageView、或其它视图组件
也可以定义一些对视图的操作方法
import UIKit
class WalkthroughPageViewController: UIPageViewController, UIPageViewControllerDataSource {
var pageHeadings = ["Personalize", "Locate", "Discover"]
var pageImages = ["foodpin-intro-1", "foodpin-intro-2", "foodpin-intro-3"]
var pageContent = ["Pin your favorite restaurants and create your own food guide", "Search and locate your favourite restaurant on Maps", "Find restaurants pinned by your friends and other foodies around the world"]
override func viewDidLoad() {
super.viewDidLoad()
dataSource = self
if let startingViewController = contentViewController(at: 0) {
setViewControllers([startingViewController], direction: .forward, animated: true, completion: nil)
} else {
print("nil")
}
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
var index = (viewController as! WalkthroughContentViewController).index
index -= 1
return contentViewController(at: index)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
var index = (viewController as! WalkthroughContentViewController).index
index += 1
return contentViewController(at: index)
}
func contentViewController(at index: Int) -> WalkthroughContentViewController? {
if index < 0 || index >= pageHeadings.count {
return nil
}
if let pageContentViewController = storyboard?.instantiateViewController(withIdentifier: "WalkthroughContentViewController") as? WalkthroughContentViewController {
pageContentViewController.heading = pageHeadings[index]
pageContentViewController.content = pageContent[index]
pageContentViewController.imageFile = pageImages[index]
pageContentViewController.index = index
return pageContentViewController
}
return nil
}
func forward(index: Int) {
if let nextViewController = contentViewController(at: index + 1) {
setViewControllers([nextViewController], direction: .forward, animated: true, completion: nil)
}
}
}
页面只在应用最初进入的时候显示一次的功能
使用UserDefaults类来存储是否显示过的状态
在最后一个内容界面按下“进入界面”后的位置使用UserDefaults.standard.set(Bool:key:)方法设置保存状态
在主页面的viewDidAppear(animated:)中检测状态
这里放出代码地址: https://github.com/dengxife/SwiftTips