Mor*_*itz 0 ios uipageviewcontroller swift
我有以下功能,每当用户从一个页面滑动到另一个页面(垂直分页)时,我想调用它:
func sendNotification (){
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "stopVideo"), object: nil)
print("called")
}
Run Code Online (Sandbox Code Playgroud)
当前,每当创建 pageAfter 或 -before 时都会调用该函数。用于“创建”下一页/上一页的函数如下所示(在本例中为viewControllerAfter):
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndexString = (viewController as! MyViewController).index
let currentIndex = indec.index(of: currentIndexString!)
//set if so that next page
if currentIndex! < indec.count - 1 {
let myViewController = MyViewController()
myViewController.index = indec[currentIndex! + 1]
sendNotification() //function is called
return myViewController
}
return nil
}
Run Code Online (Sandbox Code Playgroud)
由于 UIPageViewController 在某种程度上准备了以下页面并且返回上一页也不会调用该函数(因为不需要“创建”视图控制器)我没有得到我想要的结果。我想知道是否有一个函数可以sendNotification()在动画完成时调用。我发现了很多关于跳转到另一页的问题,但没有一个关于我的问题。我真的很感激你的帮助!
您可以使用UIPageViewControllerDelegate函数
func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController])
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool)
Run Code Online (Sandbox Code Playgroud)
正如这些函数的名称所说,第一个将在用户滑动到 PageViewController 的另一个页面时被调用。
当 Transition 完成时,将调用第二个,这意味着用户会在动画后看到下一页。
在两者中,您都可以检查页面之间的更改。
也不要忘记检查completed第二个函数的变量。如果用户开始滑动然后没有进入下一页(因为他在它之前松开手指或类似的),那是错误的,你没有新的 ViewController。
该数组previousViewControllers包含最近显示的控制器,其中previousViewControllers[0]可能是之前显示的控制器。
有关更多信息,请查阅文档
| 归档时间: |
|
| 查看次数: |
4169 次 |
| 最近记录: |