0

I have implemented scrollViewDidScroll: inside my viewcontroller to cause some animations when I scroll the view up and down.

However, when I scroll my collectionview inside the viewcontroller (horizontally) it messes up with my animation inside scrollViewDidScroll:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    newAlpha = 1 - scrollView.contentOffset.y / 200;
    self.introImageView.alpha = newAlpha;
    //... -> prevent scrolling when collectionview is scrolled
}

How do I prevent calling scrollViewDidScroll: when scrolling my collectionview horizontally?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Besfort Abazi
  • 373
  • 3
  • 18
  • 1
    maybe checking the scroll direction: https://stackoverflow.com/questions/31857333/how-to-get-uiscrollview-vertical-direction-in-swift – GIJOW Sep 27 '18 at 17:26
  • 1
    or checking the class type `- (void)scrollViewDidScroll:(UIScrollView *)scrollView { if ([scrollView isKindOfClass:[UICollectionView class]] == YES) { // do the trick } }` – GIJOW Sep 27 '18 at 17:28

2 Answers2

1

The best way is not to disable the delegate method, but make sure to only call that code when it's called by your scrollview. Here's an example

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.myScrollView) {
        newAlpha = 1 - scrollView.contentOffset.y / 200;
        self.introImageView.alpha = newAlpha;
    } else {
       //collectionView would fall here
    }

}
Matthew Knippen
  • 1,048
  • 9
  • 22
  • 1
    you can also use the method of checking the class as @GIJOW did above, but this option would work if you had multiple scroll views as well, and guarantees it will only ever be called on the class that you would like it to. – Matthew Knippen Sep 27 '18 at 17:32
  • 1
    Thanks! This works for me, as I have multiple collectionViews with different logic, thus, making this approach more neat. – Besfort Abazi Sep 27 '18 at 17:37
1
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if ([scrollView isKindOfClass:[UICollectionView class]] == NO) {
        newAlpha = 1 - scrollView.contentOffset.y / 200;
        self.introImageView.alpha = newAlpha;
        //... -> prevent scrolling when collectionview is scrolled
    }
}
GIJOW
  • 2,307
  • 1
  • 17
  • 37