我正在尝试使用 swift 4
在 page view controller
中创建 auto sliding
。在这里,我尝试了下面的代码,但我得到了结果,它重复了图像的第一个 index
。我需要知道,如何实现下一个索引的移动?
// Image array
var name = ["one.png","two.png","three.png","four.png"]
// Timer in viewdidload()
tTime = Timer.scheduledTimer(timeInterval: 4, target: self, selector: #selector(changeSlide), userInfo: nil, repeats: true)
// Timer function call (The problem is here...)
@objc func changeSlide() {
let pageContentViewController = self.viewControllerAtIndex(index: 0)
self.pageViewController.setViewControllers([pageContentViewController!], direction: UIPageViewControllerNavigationDirection.forward, animated: true, completion: nil)
}
最佳答案
// Image array
var name = ["one.png","two.png","three.png","four.png"]
var currentIndex = -1
// Timer in viewdidload()
tTime = Timer.scheduledTimer(timeInterval: 4, target: self, selector: #selector(changeSlide), userInfo: nil, repeats: true)
// Timer function call
@objc func changeSlide()
{
currentIndex = (currentIndex == name.count - 1) ? 0 : currentIndex + 1
let pageContentViewController = self.viewControllerAtIndex(index: currentIndex)
self.pageViewController.setViewControllers([pageContentViewController!], direction: UIPageViewControllerNavigationDirection.forward, animated: true, completion: nil)
}
关于ios - Swift 4 在定时器的帮助下在页面 View Controller 中自动滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52676619/