Search code examples
iosswiftuitableviewuiscrollviewuiscrollviewdelegate

ScrollViewDelegate not calling scrollViewDidScroll or scrollViewWillBeginDragging


I have taken the solution from iOS tableview how can I check if it is scrolling up or down, answering how to test if the user is scrolling up or down and have implemented into my code. In the first viewcontroller with a UITableView in it, this code worked perfectly fine, yet for the other with a similar UITableView, this does not worked. I have checked for any possible mistakes that I might have made, such as accidentally referencing these in a different function, or if I did not reference the UIScrollViewDelegate when defining my class, yet according to the solution, this is not necessary as the "UITableViewDelegate is already a subclass of UIScrollViewDelegate."

Here is my code that should be called, yet it does not (I am planning on animating certain components on the viewcontroller depending on the user moving up or down):

var lastContentOffset: CGFloat = 0

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    self.lastContentOffset = scrollView.contentOffset.y
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if (self.lastContentOffset < scrollView.contentOffset.y) {
        UIView.animate(withDuration: 0.2, animations: { () -> Void in
            self.listTableView.frame = CGRect(x:0 ,y: 36,width: self.view.bounds.width,height: self.view.bounds.height - 36)
        })



    } else if (self.lastContentOffset > scrollView.contentOffset.y) {
        UIView.animate(withDuration: 0.2, animations: { () -> Void in
            self.listTableView.frame = CGRect(x:0 ,y: 131,width: self.view.bounds.width,height: self.view.bounds.height - 131)               
        })

    } else {

    }
}

So far, I have tried printing a text simply to see if it might just be an error with my animation, but nothing happens. Here is some more background information about the rest of my code:

  1. I did implement two UISwipeGestures and one UITapGesture for another functionality in the viewController

  2. I define and set up the tableView before this code, yet that should not make a difference

  3. The information my tableView gathers is through a few web requests, which does not take long but still some time - I have written a function for it

  4. At times, a blurViewController and a ContainerViewController completely cover the tableView, yet I have developed a code to set these into the background upon request to not impair the functionality of the tableView

  5. This is not a UITableViewController but a UITableView that partially covers the UIViewController


Solution

  • did you set the scrollView delegate to self ?


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.scrollView.delegate = self
    
    }