I have a ScrollView on a view, where the page control is sticky from the bottom. The page control is animated in and shown when the page is scrolled to 48px from the bottom of the page and stays opacity 100% (shown) to the end of the page. But I am unable to detect this behaviour. scrollView content Size is mentioned below.

ScrollView content Size:

width : 414.0 height : 852.0

I am using the below code.

//MARK: -  ScrollView Delegate
extension RemoveViewController: UIScrollViewDelegate {

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let distanceFromBottom = scrollView.contentSize.height - scrollView.contentOffset.y
        print(distanceFromBottom)
        if distanceFromBottom >= 796 {
            print(" you reached at desired bottom")
            self.showPageControl(toShow: true)
        } else {
            self.showPageControl(toShow: false)
        }
    }
}


func showPageControl(toShow: Bool) {
    if toShow {
        if self.pageControl.alpha != 1 {
            UIView.animate(withDuration: 0.300, animations: {
                self.pageControl.alpha = 1
            })
        }
    } else {
        if self.pageControl.alpha != 0 {
            UIView.animate(withDuration: 0.300, animations: {
                self.pageControl.alpha = 0
            })
        }
    }
}

Kindly let me know what I am doing incorrectly here.

1

There are 1 best solutions below

3
Ramy Rizkalla On BEST ANSWER

You need to calculate the bottomOffset first, then calculate the difference between it and the contentOffset. If it's <= 56, then you reached a specific position.

let bottomOffset = scrollView.contentSize.height - scrollView.frame.size.height

let position = bottomOffset - scrollView.contentOffset.y

if position <= 56 {
    print(" you reached at desired bottom")
    self.showPageControl(toShow: true)
} else {
    self.showPageControl(toShow: false)
}