0

I would like to find out which page is visible to the user and also do a certain action when a change in the page is detected. To do this I am using the function scrollViewDidScroll. As I didn't get the result I wanted (nothing happened), I looked for answers on stackoverflow and found the following question which was answered: Detecting UIScrollView page change, to be more precise I used the answer by Michael Waterfall and converted the Objective-C code to swift code.

Below you will find the code I used to create the scrollView in my class HomeController: UIViewController, UIScrollViewDelegate {. Moreover the function by Michael Waterfall is declared and called in the function initiateScrollView(). However, the function doesn't print anything. Do you know where I am doing something wrong? I can't see where I made a mistake. Nothing happens when I scroll (e.g. from page 1 to page 2 (homeView to view2)). The function only prints before: 0 once and after that nothing happens.

Code

override func viewDidLoad() {

    super.viewDidLoad()
    view.backgroundColor = .white

    initiateScrollView()

}

func initiateScrollView() {

    //create scrollView with paging enabled
    let scrollView = UIScrollView(frame: view.bounds)
    scrollView.isPagingEnabled = true
    view.addSubview(scrollView)

    //get page size
    let pageSize = view.bounds.size

    //individual views
    let homeView = UIView()
    homeView.backgroundColor = .green

    let view2 = UIView()
    view2.backgroundColor = .blue

    let view3 = UIView()
    view3.backgroundColor = .red

    //array with individual views
    let pagesViews = [homeView, view2, view3]

    //amount of views
    let numberOfPages = pagesViews.count
    print(numberOfPages) //prints '3'

    //add subviews (pages)
    for (pageIndex, page) in pagesViews.enumerated(){

        //add individual pages to scrollView
        page.frame = CGRect(origin: CGPoint(x:0 , y: CGFloat(pageIndex) * pageSize.height), size: pageSize)
        scrollView.addSubview(page)

    }

    func scrollViewDidScroll(scrollView: UIScrollView) {

        var previousPage = 0
        let fractionalPage: Float = Float(scrollView.contentOffset.y / pageSize.height)
        let page = lround(Double(fractionalPage))

        print("before:", page) //prints 'before: 0' once

        if previousPage != page {

            // Page has changed, do your thing!
            // ...
            // Finally, update previous page
            print("before:", page)
            previousPage = page
            print("after:", page)

        } //never prints anything

    }

    scrollViewDidScroll(scrollView: scrollView)

    //define size of scrollView
    scrollView.contentSize = CGSize(width: pageSize.width, height: pageSize.height * CGFloat(numberOfPages))
}
Community
  • 1
  • 1
Moritz
  • 745
  • 1
  • 10
  • 32

2 Answers2

2

You forgot to set the viewController as the delegate for the scroll view, as following:

func initiateScrollView() {

//create scrollView with paging enabled
let scrollView = UIScrollView(frame: view.bounds)
scrollView.isPagingEnabled = true
scrollView.delegate = self
view.addSubview(scrollView)
(...) } 

And I recommend that you use the scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) function, instead of scrollViewDidScroll(...)

  • No, it still won't print anything. – Moritz May 19 '17 at 19:23
  • 1
    You should delete the standalone line `scrollViewDidScroll(scrollView: scrollView)` (before the`contentSize` statement), and remove the function `func ScrollViewDidScroll(...) {...}` and put it inside the viewController class, not in the `initiateScrollView()` function. Don't forget to set the delegate also. – Fayez Hellani May 19 '17 at 19:33
  • But then I would need to make `pageSize.height` global, wouldn't I? – Moritz May 19 '17 at 19:38
  • 1
    Yes. But you can discard `pageSize.height` and use its proper value, `self.view.bounds.size.height` – Fayez Hellani May 19 '17 at 19:43
  • But where do I call the function then? I can't just put it in the class and not call it...? EDIT: XCode corrected the last mistake: `func scrollViewDidScroll(_ scrollView: UIScrollView) {` (the underscore...) – Moritz May 19 '17 at 19:50
  • 2
    You do not call this function, it gets automatically called when you scroll the scrollview, as its name suggests. – Fayez Hellani May 19 '17 at 19:55
0

This is because you haven't set the delegate property of your scrollview. Add scrollView.delegate = self in your initiateScrollView method.