在UIPageViewController中更改页面自动使用swift 3中的计时器? [英] change pages in UIPageViewController Automatically with timer in swift 3?

查看:211
本文介绍了在UIPageViewController中更改页面自动使用swift 3中的计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 UIPageViewController 包含4个视图控制器

I have UIPageViewController that Contains 4 view Controllers

我设置了自动更改视图控制器的计时器(进行中)到下一个视图控制器) - 这个计时器和方法将工作但问题是,它的工作原理只是第一个幻灯片,当应用程序将运行,并在5秒后, UIPageViewController 将显示第二张幻灯片但之后什么都不会发生!
这是我使用的代码

I set the timer for changing view controllers automatically(going to the next view controllers) - This timer and method will work But the problem is that It works Just for first Slide and when the app will run and after 5 seconds the UIPageViewController will show the second slide But after that nothing will happen ! This Is the Code That I used

lazy var VCArr : [UIViewController] = {

    return [self.VCInstance(name : "VC1"),
            self.VCInstance(name : "VC2"),
            self.VCInstance(name :"VC3"),
            self.VCInstance(name :"VC4")]
}()

func changeSlide() {

    print("Slide Change!")


    guard let currentViewController = self.VCArr.first  else { return }

            guard let nextViewController = dataSource?.pageViewController( self, viewControllerAfter: currentViewController ) else { return }

            setViewControllers([nextViewController], direction: .forward, animated: true, completion: nil)

    }

var tTime: Timer!

override func viewDidLoad() {
    super.viewDidLoad()


    tTime = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(changeSlide), userInfo: nil, repeats: true)


    }

我认为问题出在这一行

guard let currentViewController = self.VCArr.first  else { return }

但我不知道如何更改此行以正常工作

But I don't know how to change this line to work fine

推荐答案

试试这个:

您可以按VCArr的索引更改视图控制器。

You can change view controller by index of VCArr.

var index = 0

func changeSlide() {
    index + 1
    if index < self.VCArr.count {
        setViewControllers([VCArr[index]], direction: .forward, animated: true, completion: nil)
    }
    else {
        index = 0
        setViewControllers([VCArr[0]], direction: .forward, animated: true, completion: nil)

    }
}

这篇关于在UIPageViewController中更改页面自动使用swift 3中的计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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