【问题标题】:How detect which UICollectionView scrolled?如何检测哪个 UICollectionView 滚动了?
【发布时间】:2021-05-09 12:47:31
【问题描述】:

我在一个控制器中有多个 collectionView。其中一个在顶部,第二个在底部。所以我的问题是如何检测哪个collectionView 视图被滚动?(在Objective-c 中)为了检测滚动,我使用以下方法: - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 。但是在这种方法中,我无法检测到哪个 collectionView 被滚动。请帮助您的建议。

【问题讨论】:

    标签: ios objective-c uicollectionview uiscrollview


    【解决方案1】:

    UICollectionView 是 UIScrollView 的子类,所以你可以比较一下

    https://developer.apple.com/documentation/uikit/uicollectionview

    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
    {
        if (scrollView == collectionViewOne) {
    
        }else if (scrollView == collectionViewTwo) {
    
        }else{
                //something else
        }
    }
    

    【讨论】:

      【解决方案2】:

      创建你的 collectionView 的 outlets。

      @IBOutlet weak var topCollectionView: UICollectionView!
      @IBOutlet weak var bottomCollectionView: UICollectionView!
      

      在scrollView的Delegate方法中:

      - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
          if (scrollView == topCollectionView) {
      
          }
          else if (scrollView == bottomCollectionView) {
      
          }
      }
      

      这将起作用,因为 UICollectionView 是 UIScrollView 的子类。

      【讨论】:

        【解决方案3】:

        Swift 中你可以这样检查:

        func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
                
                if scrollView == topCollectionViewName {
                    print("Call Top CollectionView")
                }else if scrollView == bottomCollectionViewName{
                    print("Call Bottom CollectionView")
                }else{
                    print("call any other")
                }
                
        }
        

        Objective C中你可以这样检查:

        -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
        
            if (scrollView == topCollectionViewName) {
                 NSLog(@"Call Top CollectionView");
            }else if (scrollView == bottomCollectionViewName) {
                 NSLog(@"Call Bottom CollectionView");
            }else{
                    NSLog(@"Call any other");
            }
        }
        

        很高兴为您提供帮助:)

        【讨论】:

        • @TolibovUmid 很高兴为您提供帮助。如果此答案符合您的问题,请标记为正确答案,以便对功能中的其他人有所帮助。
        【解决方案4】:
        //        create outlets of both collectionview
                @IBOutlet weak var collectionViewA: UICollectionView!
                @IBOutlet weak var collectionVieB: UICollectionView!
                
        //        add delegate method to detect scroll of collectionview
        //         for swift
                func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
                    
                    if collectionView == collectionViewA{
                        // collectionviewA was scrolled
                    }else{
                        // collectionviewB was scrolled
                    }
                    
                }
                
        //        same deleagte for objc
                
                - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
                    // compare both collectionview here like upper swift method
                }
                
        

        【讨论】:

          猜你喜欢
          • 2014-08-20
          • 1970-01-01
          • 2017-05-02
          • 2013-05-24
          • 1970-01-01
          • 1970-01-01
          • 2014-08-05
          • 1970-01-01
          • 2022-01-26
          相关资源
          最近更新 更多