Search code examples
iosswiftuiscrollviewuiscrollviewdelegate

UIScrollView Delegate Re-Routing - To check the state of UIScrollView


I have a tableView and button on the same view (Siblings),

i need to HIDE the button when the tableView is SCROLLING (Up and Down Direction no problem) and SHOW when it is STOPPED SCROLLING

I tried the following code, but it is not working (Correctly)

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    self.vcView.hideButton()
 }

 func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
    self.vcView.showButton();
 }

After a lot of search i found this article, but it is in Objective C, can anyone help me to convert it in to Swift 3.


Solution

  • Why not simply implement these two delegate methods:

    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        self.vcView.hideButton()
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        self.vcView.showButton();
    }
    

    If you're only looking to show and hide a button or any view while scrolling, this will work. Just be sure to set the scrollview delegate and it should work without issues.
    Theres no need to convert that objc code to swift.

    https://screencast.com/t/eTq9Nzfsgs6E