【问题标题】:How can I detect the scroll direction from the UICollectionView?如何从 UICollectionView 检测滚动方向?
【发布时间】:2014-08-20 06:03:51
【问题描述】:

我有一个 UICollectionView。我想检测滚动方向。我有两种不同的动画风格用于向下滚动和向上滚动。所以我必须学习滚动方向。

CGPoint scrollVelocity = [self.collectionView.panGestureRecognizer
velocityInView:self.collectionView.superview];

if (scrollVelocity.y > 0.0f)   
NSLog(@"scroll up");

else if(scrollVelocity.y < 0.0f)    
NSLog(@"scroll down");

这只是手指触摸的工作。不适合我

【问题讨论】:

    标签: ios objective-c uicollectionview uigesturerecognizer uipangesturerecognizer


    【解决方案1】:

    试试这个:

    在你的标题中添加这个:

    @property (nonatomic) CGFloat lastContentOffset;
    

    然后重写scrollViewDidScroll:方法:

    #pragma mark - UIScrollViewDelegate
    
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        if (self.lastContentOffset > scrollView.contentOffset.y)
        {
            NSLog(@"Scrolling Up");
        }
        else if (self.lastContentOffset < scrollView.contentOffset.y)
        {
            NSLog(@"Scrolling Down");
        }
    
        self.lastContentOffset = scrollView.contentOffset.y;
    }
    

    发现于Finding the direction of scrolling in a UIScrollView?

    【讨论】:

      【解决方案2】:

      我试图找到一种方法来检测用户是否主要尝试垂直或水平拉动滚动视图。我给你我的解决方案,我希望它对任何人都有用:

      CGPoint _lastContentOffset;
      
      - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
          _lastContentOffset = scrollView.contentOffset;
      }
      
      - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
      
          if (ABS(_lastContentOffset.x - scrollView.contentOffset.x) < ABS(_lastContentOffset.y - scrollView.contentOffset.y)) {
              NSLog(@"Scrolled Vertically");
          } else {
              NSLog(@"Scrolled Horizontally");
          }
      
      }
      

      这项工作为我找到,我用它来避免在垂直和相反滚动时滚动视图水平移动。

      【讨论】:

        【解决方案3】:

        这是获取滚动方向的最佳方法,希望对您有所帮助

        - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
        
            CGPoint targetPoint = *targetContentOffset;
            CGPoint currentPoint = scrollView.contentOffset;
        
            if (targetPoint.y > currentPoint.y) {
                NSLog(@"up");
            }
            else {
                NSLog(@"down");
            }
        }
        

        【讨论】:

        • 拖动...不就是用手指拖动吗?滚动甚至可能在没有用户手指的情况下发生,不是吗?
        【解决方案4】:

        斯威夫特 4.2

        private var lastContentOffset: CGFloat = 0
        func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
            if lastContentOffset > scrollView.contentOffset.y && lastContentOffset < scrollView.contentSize.height - scrollView.frame.height {
                // move up
                print("move up")
                originalHeight ()
            } else if lastContentOffset < scrollView.contentOffset.y && scrollView.contentOffset.y > 0 {
                // move down
                print("move down")
                minimizeHeaderView()
            }
        
            // update the new position acquired
            lastContentOffset = scrollView.contentOffset.y
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-11-01
          • 1970-01-01
          • 2021-05-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多