判断UIScrollView滑动方向可以借助UIScrollView的delegate函数来实现,下面的例子可以检测到UIScrollview当前是向上滑动还是向下滑动:
int _lastPosition; //A variable define in headfile
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
int currentPostion = scrollView.contentOffset.y;
if (currentPostion - _lastPosition > 25) {
_lastPosition = currentPostion;
NSLog(@"ScrollUp now");
}
else if (_lastPosition - currentPostion > 25)
{
_lastPosition = currentPostion;
NSLog(@"ScrollDown now");
}
}
说明:这里的25可以换成任意数字,可根据自己的需要来设定。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//Selected index's color changed.
static float newx = 0;
static float oldIx = 0;
newx= scrollView.contentOffset.x ;
if (newx != oldx ) {
//Left-YES,Right-NO
if (newx > oldx) {
self.scrollLeftOrRight = NO;
}else if(newIndex < oldIndex){
self.scrollLeftOrRight = YES;
}
oldx = newx;
}
}