根据UIScrollView 滑动获取当前页数

#pragma mark --
#pragma mark --  UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    CGFloat pageWidth = mainScrollView.frame.size.width;
    
    int currentPage = floor((mainScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
   

    if (currentPage == _fullImagesArray.count - 1) {
        NSLog(@"---最后一页---");
    }else if (currentPage == 0){
        NSLog(@"---第一页---");
    }else{
        NSLog(@"---中间页---");
    }
    
}

 

 

 

#pragma mark - UIScrollViewDelegate
#pragma mark - 左右滑动事件
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    static float newX = 0;
    
    if(scrollView == wmScrollView) {
        CGFloat pageWidth = wmScrollView.frame.size.width;
        int currentPage = floor((wmScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
        pageControl.currentPage = currentPage;
       
        NSArray *wmScrollViewSubViews = [wmScrollView subviews];
        NSInteger count = [wmScrollViewSubViews count];
        
        NSLog(@"------ currentPage :%d , count:%d",(int)currentPage,(int)count);
        
//        if (currentPage == count) {
//        float itemWidth = Main_Screen_Width/5.0;
//            NSLog(@"----移动---");
//            //移动下面的 tabbarScrollView
//            [tabbarScrollView setContentOffset:CGPointMake(pageWidth, 0) animated:YES];
//        }
        
        //判断左右滑动
        newX = scrollView.contentOffset.x;
        if (newX != _oldX) {
            if (newX > _oldX && (newX - _oldX) > 100) {
                NSLog(@"左");
                _oldX = newX;
                if (currentPage == count-1) {
                    //切换
                    NSLog(@"左切换");
                }
                
            } else if (newX < _oldX && (_oldX - newX) > 100) {
                NSLog(@"右");
                _oldX = newX;
                if (currentPage == 0) {
                    //切换
                    NSLog(@"右切换");
                }

            }
        }
        
    }
}

posted on 2015-05-18 16:25  奋进的闹钟  阅读(3072)  评论(0编辑  收藏  举报

导航