0

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

Al E. Mone
  • 97
  • 2
  • 14

2 Answers2

2

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

}
  • Thank you so much. What ended up working was 'self.listTableView.delegete = self'. What is weird, however, is that I did not do this in my other code, which works too. One question: Why do I need to do this? It seems to me to be redundant, but I seem to be mistaken. – Al E. Mone Jan 31 '18 at 18:15
  • @CyprianZander by doing delegate = self you are confirming to the protocol of scrollView Delegate methods. In other words your are telling your controller that you want to receive callbacks of the functions associated with this protocol. – Nimish Sharma Jan 31 '18 at 18:19
  • Thank you very much, that made this step a lot clearer! – Al E. Mone Feb 01 '18 at 19:34
0
To use delegate method of the UIScrollView, you have to set the delegate for it

You can set the delegate method in two ways:-

1) By Storyboard - Select UIScrollView and Drag and drop to File Owner of the View Controller the delegate method will set for it.

2) By Code - In ViewDidLoad() method you can use the following code:-

- (void) viewDidLoad
{
  [super viewDidLoad];
  // Do any additional setup after loading the view.
  self.scrollview.delegate = self;
}

In this way your ScrollView delegate method will be called. 
Swati Gautam
  • 199
  • 9