如何检测scrollView滚动方向?

时间:2015-10-05 04:03:37

标签: ios objective-c uitableview uiscrollview

我内部有一个UIScrollView我有一个UITableView滚动视图应该水平滚动。当滚动视图结束其水平滚动时,我已实现- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView来执行某些任务。但我的问题是,即使UITableView垂直滚动也停止,此委托也会触发。那么如何只检测此委托中的scrollview水平滚动?

请帮帮我, 感谢

2 个答案:

答案 0 :(得分:12)

检查这些委托方法

CGPoint _lastContentOffset;

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  _lastContentOffset = scrollView.contentOffset;
}

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

    if (_lastContentOffset.x < (int)scrollView.contentOffset.x) {
      NSLog(@"Scrolled Right");
    }
    else if (_lastContentOffset.x > (int)scrollView.contentOffset.x) {
      NSLog(@"Scrolled Left");
    }

    else if (_lastContentOffset.y < scrollView.contentOffset.y) {
      NSLog(@"Scrolled Down");
    }

    else if (_lastContentOffset.y > scrollView.contentOffset.y) {
      NSLog(@"Scrolled Up");
    }
}

答案 1 :(得分:0)

快速版本:

  var _lastContentOffset: CGPoint!

  func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
     _lastContentOffset = scrollView.contentOffset
  }

  func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

    if _lastContentOffset.x < scrollView.contentOffset.x {
      NSLog("Scrolled Right")
    }
    else if _lastContentOffset.x > scrollView.contentOffset.x {
      NSLog("Scrolled Left")
    }

    else if _lastContentOffset.y < scrollView.contentOffset.y {
      NSLog("Scrolled Down")
    }

    else if _lastContentOffset.y > scrollView.contentOffset.y {
      NSLog("Scrolled Up")
    }
}