Swift 4 在计时器的帮助下在页面视图控制器中自动滑动
Swift 4 auto slide in page view controller with help of timer
我正在尝试使用 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)
}
我正在尝试使用 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)
}