简体   繁体   中英

Disable scrollViewDidScroll: when scrolling UICollectionView - iOS

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?

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
    }

}
- (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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM