在UIPageViewController中跟踪页面索引的可靠方法-Swift [英] Reliable way to track Page Index in a UIPageViewController - Swift

查看:239
本文介绍了在UIPageViewController中跟踪页面索引的可靠方法-Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在常规UIViewController中的容器内有一个UIPageViewController(自定义)。我需要能够在每次页面更改时调用一个事件,但只有在它确实发生更改时才可以调用事件,而不仅是中途更改或任何此类更改。

I have a UIPageViewController (custom one) inside a Container located in a regular UIViewController. i need to be able to call an event with each Page Change but ONLY if it really did change and not only half way or anything of that sort.

使用:

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?

不可靠,由于某种原因每次都不被调用。

is unreliable and not called each time for some reason.

如果您的答案包含有关willTransitionToViewControllers或didFinishAnimating的任何内容,请详细说明而不仅仅是提及它们,因为我已经知道它们的存在,但不了解使用它们的正确方法。

if your answer contains anything about willTransitionToViewControllers or didFinishAnimating please elaborate and not just mention them, since i already know they exist but dont understand the proper way to use them.

谢谢

推荐答案

使用 didFinishAnimating 它具有完成完成属性,因此您知道页面实际上已更改。从pageViewController可以获取当前显示的页面,然后获取该VC在模型中的位置。

Use didFinishAnimating it has a completed and finished property so you know the page has actually changed. From the pageViewController you can get the currently displayed page, then get the position of this VC in your model.

首先请确保ViewController采用 UIPageViewControllerDelegate

First make sure your ViewController adopts UIPageViewControllerDelegate

设置委托(例如在 viewDidLoad 中)

pageViewController.delegate = self

然后执行以下功能:

    func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        if (completed && finished) {
            if let currentVC = pageViewController.viewControllers?.last {
                let index = myViewControllers.indexOf(currentVC)
                //do something with index
            }
        }
    }

这篇关于在UIPageViewController中跟踪页面索引的可靠方法-Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆