您好我只想在swift 3中使用计时器自动滚动页面视图控制器中的页面 - 所以在我阅读类似的问题后,我使用此扩展代码在页面视图控制器中自动滚动但是它没有对我有效
这是扩展代码
extension pageVC2 {
func goToNextPage(animated: Bool = true, completion: ((Bool) -> Void)? = nil) {
if let currentViewController = viewControllers?[0] {
if let nextPage = dataSource?.pageViewController(self, viewControllerAfter: currentViewController) {
setViewControllers([nextPage], direction: .forward, animated: animated, completion: completion)
}
}
}
}
然后我尝试了另一个答案,所以我使用了这段代码但是我会得到ScrollView的错误
这是我尝试的第二种方式
func moveToNextPageTwoSecond (){
let pageWidth:CGFloat = CGRectGetWidth(self.scrollView.frame)
let maxWidth:CGFloat = pageWidth * 4
let contentOffset:CGFloat = self.scrollView.contentOffset.x
var slideToX = contentOffset + pageWidth
if contentOffset + pageWidth == maxWidth{
slideToX = 0
}
self.scrollView.scrollRectToVisible(CGRectMake(slideToX, 0, pageWidth, CGRectGetHeight(self.scrollView.frame)), animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(pageVC2.moveToNextPageTwoSecond), userInfo: nil, repeats: true)
}