mapanguan

导航

如何判断ScrollView滑动方向

1/判断滚动视图左右滚动

   {
    CGFloat startContentOffsetX;//滚动开始的坐标
    CGFloat willEndContentOffsetX; //滚动即将停止的坐标
    CGFloat endContentOffsetX;//滚动结束的坐标
 }

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ myPC.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width; currNum = (int)myPC.currentPage; } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ startContentOffsetX = scrollView.contentOffset.x; [self stopTimer]; } - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{ //将要停止前的坐标 willEndContentOffsetX = scrollView.contentOffset.x; } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ endContentOffsetX = scrollView.contentOffset.x; if (endContentOffsetX < willEndContentOffsetX && willEndContentOffsetX < startContentOffsetX) { //画面从右往左移动,前一页 isNext = NO; } else if (endContentOffsetX > willEndContentOffsetX && willEndContentOffsetX > startContentOffsetX) {//画面从左往右移动,后一页 isNext = YES; } [self starTimer]; }

2/判断上下滚动视图上下滚动

float lastContentOffset;
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    lastContentOffset = scrollView.contentOffset.y; 
}
 
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    if (lastContentOffset < scrollView.contentOffset.y) {
        NSLog(@"向上滚动");
    }else{
        NSLog(@"向下滚动");
    }
}

  

  

posted on 2016-09-08 17:50  mapanguan  阅读(4970)  评论(0编辑  收藏  举报