检测UIScrollView(UITableView)的滚动方向

有时候我们需要检测当前UIScrollView的滑动方向来做出相应的处理,可以借助UIScrollView的delegate函数来实现, 下面的例子可以检测到UIScrollview当前是向上滑动还是向下滑动:

  1.  int _lastPosition;    //A variable define in headfile  
  2.   
  3. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{  
  4.     int currentPostion = scrollView.contentOffset.y;  
  5.     if (currentPostion - _lastPosition > 25) {  
  6.         _lastPosition = currentPostion;  
  7.         NSLog(@"ScrollUp now");  
  8.     }  
  9.     else if (_lastPosition - currentPostion > 25)  
  10.     {  
  11.         _lastPosition = currentPostion;  
  12.         NSLog(@"ScrollDown now");  
  13.     }  
  14. }  

25 可以是任意数字,可根据自己的需要来设定。


而UItableView是UIscrollView的子类,自然有UIscroolView的特性。如果判断UItableView的滚动方向,如下

  1. int _lastPosition;    //A variable define in headfile  
  2.   
  3. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{  
  4.     int currentPostion = tableView.contentOffset.y;  
  5.     if (currentPostion - _lastPosition > 25) {  
  6.         _lastPosition = currentPostion;  
  7.         NSLog(@"ScrollUp now");  
  8.     }  
  9.     else if (_lastPosition - currentPostion > 25)  
  10.     {  
  11.         _lastPosition = currentPostion;  
  12.         NSLog(@"ScrollDown now");  
  13.     }  
  14. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值