0

Hi I just want to scroll pages in page view controller automatically with timer in swift 3 - so after I read similar questions I used this Extension code to auto scroll in page view controller But It Didn't worked for me

here is the Extension Code

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)
        }
    }
}
}

and then I tried Another answer so I used This code But I will get Error with ScrollView

Here is the second way that I tried

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)
 }
Saeed Rahmatolahi
  • 1,317
  • 2
  • 27
  • 60

0 Answers0