0

Hi I am using the PageController in my application.

I have inserted all the data on scrollView.

What I want do is, I want to move the scrollview automatically like page control with specific time.

When the scrollView reaches to last page it again recall all pages speedy and start from first onwards.

I want to load/get first page very smoothly after last page completion.

Find my code with following.

var slides:[Slide] = [];
var offSet: CGFloat = 0
override func viewDidLoad() {
    super.viewDidLoad()

    slides = createSlides()
    setupSlideScrollView(slides: slides)

    pageControl.numberOfPages = slides.count
    pageControl.currentPage = 0
    view.bringSubview(toFront: pageControl)

    let timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(autoScroll), userInfo: nil, repeats: true)

}

@objc func autoScroll() {
    let totalPossibleOffset = CGFloat(slides.count - 1) * self.view.bounds.size.width
    if offSet == totalPossibleOffset {
        offSet = 0 // come back to the first image after the last image
    }
    else {
        offSet += self.view.bounds.size.width
    }
    DispatchQueue.main.async() {
        UIView.animate(withDuration: 0.3, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: {
            self.scrollView.contentOffset.x = CGFloat(self.offSet)
        }, completion: nil)
    }
}
Himanshu padia
  • 7,428
  • 1
  • 47
  • 45
srikanth kumar
  • 77
  • 1
  • 10
  • Possible duplicate of [How to create a Scroll View with a page control using swift?](https://stackoverflow.com/questions/29074454/how-to-create-a-scroll-view-with-a-page-control-using-swift) – Nikunj Kumbhani Apr 18 '19 at 04:55
  • I already seen here but I getting confused here let subView = UIView(frame: frame) subView.backgroundColor = slides[index] I need to display slides array here – srikanth kumar Apr 18 '19 at 05:03
  • So you just add image view as a subview instead of Uiview – Nikunj Kumbhani Apr 18 '19 at 05:08
  • @srikanthkumar Please replace the term Pagecontroller with `page control` or `UIPageControl` a controller is different than a control. – Amber K Apr 18 '19 at 05:17

2 Answers2

1
@objc func autoScroll() {
    let totalPossibleOffset = CGFloat(slides.count - 1) * self.view.bounds.size.width
    if offSet == totalPossibleOffset {
        offSet = 0 // come back to the first image after the last image
    }
    else {
        offSet += self.view.bounds.size.width
    }
    DispatchQueue.main.async() {
        UIView.animate(withDuration: 0.1, delay: 0, options: UIView.AnimationOptions.curveLinear, animations: {
            self.scrollView.scroll(topOffset:offSet, animated: true)
        }, completion: nil)
    }
}


extension UIScrollView {

    // Bonus: Scroll
    func scroll(topOffset:Float, animated: Bool) {
        let ofSetValue = CGPoint(x: topOffset, y: 0)
        setContentOffset(ofSetValue, animated: animated)
    }
}
Bhavesh Nayi
  • 705
  • 4
  • 15
0

Use the following code working fine.

 @objc func autoScroll() {
        let totalPossibleOffset = CGFloat(slides.count - 1) * self.view.bounds.size.width
        if offSet == totalPossibleOffset {
            offSet = 0 // come back to the first image after the last image
        }
        else {
            offSet += self.view.bounds.size.width
        }
        DispatchQueue.main.async() {
            UIView.animate(withDuration: 0.0, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: {
                self.scrollView.contentOffset.x = CGFloat(self.offSet)
            }, completion: nil)
        }
    }
srikanth kumar
  • 77
  • 1
  • 10