首页 > 解决方案 > 禁用 scrollViewDidScroll:滚动 UICollectionView 时 - iOS

问题描述

我已经实现了 scrollViewDidScroll: 在我的视图控制器中,当我上下滚动视图时会产生一些动画。

但是,当我在 viewcontroller(水平)内滚动我的 collectionview 时,它会弄乱我在 scrollViewDidScroll 内的动画:

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

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

如何防止调用 scrollViewDidScroll: 水平滚动我的 collectionview 时?

标签: iosobjective-cuicollectionview

解决方案


最好的方法不是禁用委托方法,而是确保仅在滚动视图调用该代码时才调用该代码。这是一个例子

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

}

推荐阅读