ios - Swift 4 在定时器的帮助下在页面 View Controller 中自动滑动

标签 ios swift uipageviewcontroller

我正在尝试使用 swift 4page 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/

相关文章:

ios - 在仅在最后一个 UIImageview 上可见的 PageViewController 上添加 UIButton

ios - 无法以编程方式移动 UITextView

ios - HTTP 请求在 swift 中不起作用

string - Swift 生成器 : What is the counterpart to next()?

ios - 大 UIImage 未显示在 UIImageView 中

ios - Swift:UIPageViewController - 加载单独的 View

ios - 'NSInternalInconsistencyException',原因 : 'Unexpected subviews' possibly in UIPageViewController (swift, iOS)

javascript - iOS 8 移除了 "minimal-ui"视口(viewport)属性,还有其他 "soft fullscreen"的解决方案吗?

ios - 如何快速调整表格 View 标题的高度?

ios - 如何在 Xcode 7 中将四个按钮对齐死点?