外部 UIScrollView 的 scrollViewDidScroll 内的 scrollEnabled 需要两次滑动

Posted

技术标签:

【中文标题】外部 UIScrollView 的 scrollViewDidScroll 内的 scrollEnabled 需要两次滑动【英文标题】:scrollEnabled inside Outer UIScrollView's scrollViewDidScroll requires two swipe 【发布时间】:2017-08-19 12:12:57 【问题描述】:

我在 UITableViewCell 中有 UICollectionView。我想根据两者的 contentOffset 启用或禁用 UICollectionViewUITableView 的滚动。例如,在 UICollectionView 的 ViewController 我有一个代码 -

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
    if (scrollView.contentOffset.y == 0) 
        _collectionView.scrollEnabled = false;
        // This will enable _tableView scroll which is implemented in UITableView's ViewController
        [_delegate toggleScroll:true];

     else 
        _collectionView.scrollEnabled = true;
       // This will disable _tableView scroll which is implemented in UITableView's ViewController
        [_delegate toggleScroll:false];
    


但启用滚动不会立即生效。第一次滚动不会启用或禁用 _collectionView 但在第二次滚动时它会按预期工作。我们不能即时启用滚动(仅在一次滑动/滚动时)?

【问题讨论】:

【参考方案1】:

scrollViewDidScroll 来回调用集合视图的弹跳动画。出于这个原因,一旦它的设置scrollEnabled = false 在动画后设置scrollEnabled = true。 尝试检查 0 到 10 或某个阈值之类的范围。

或者你可以试试这个:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
    if (scrollView.contentOffset.y <= 10) 
        _collectionView.scrollEnabled = false;

     else 
        _collectionView.scrollEnabled = true;

    


【讨论】:

感谢您的回答,但这不是因为阈值。 _collectionView.scrollEnabled = true/false 会立即调用,但为了看到效果,我需要结束滚动并再次滚动。这次它将按预期工作。

以上是关于外部 UIScrollView 的 scrollViewDidScroll 内的 scrollEnabled 需要两次滑动的主要内容,如果未能解决你的问题,请参考以下文章