利用UIScrollView实现循环滚动翻页的方法

首先,新建一个PageView.xib,在该xib的View里放上一个UILabel,并在IB里把它的tag设置为1:



然后,新建一个带xib文件的Controller,在这个Controller的xib里的View放上一个UIScrollView,frame和View一样大:



把刚才那个UIScrollView关联到Controller文件里,再开始写initView函数,初始化三个UIView,把他们添加到UIScrollView,然后设置UIScrollView宽度为三页,并让UIScrollView显示第二页:

_pageFirst = [[[NSBundle mainBundle] loadNibNamed:@"PageView" owner:self options:nil] lastObject];
    UILabel *labelTitle = (UILabel *)[_pageFirst viewWithTag:1];
    labelTitle.text = @"fistPage";
    
    _pageSecond = [[[NSBundle mainBundle] loadNibNamed:@"PageView" owner:self options:nil] lastObject];
    labelTitle = (UILabel *)[_pageSecond viewWithTag:1];
    labelTitle.text = @"secondPage";
    
    _pageThird = [[[NSBundle mainBundle] loadNibNamed:@"PageView" owner:self options:nil] lastObject];
    labelTitle = (UILabel *)[_pageThird viewWithTag:1];
    labelTitle.text = @"thirdPage";
    
    [self setPageFrame];
    [_scrollView addSubview:_pageFirst];
    [_scrollView addSubview:_pageSecond];
    [_scrollView addSubview:_pageThird];
    
    _scrollView.pagingEnabled = YES;
    _scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width * 3, _scrollView.frame.size.height);
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.delegate = self;
    
    //scroll to the second page
    CGPoint p = CGPointZero;
    p.x = _scrollView.frame.size.width;
    [_scrollView setContentOffset:p animated:NO];


下面是setPageFrame方法:

         

_pageFirst.frame = CGRectMake(_scrollView.frame.origin.x, _scrollView.frame.origin.y, _scrollView.frame.size.width, _scrollView.frame.size.height);
    _pageSecond.frame = CGRectMake(_scrollView.frame.origin.x + _scrollView.frame.size.width, _scrollView.frame.origin.y, _scrollView.frame.size.width, _scrollView.frame.size.height);
    _pageThird.frame = CGRectMake(_scrollView.frame.origin.x + _scrollView.frame.size.width * 2, _scrollView.frame.origin.y, _scrollView.frame.size.width, _scrollView.frame.size.height);

接下来实现UIScrollView的delegate:- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView。首先通过UIScrollView的contentOffset和frame的width属性计算得到UIScrollView滚动停止时候的显示页面,如果是第二页,则说明没有翻页,不作处理,如果是第一页或者第三页,则UIScrollView的三个subView的顺序依次向右或向左移动,并且继续让UIScrollView显示第二页:

    

CGFloat pageWidth = scrollView.frame.size.width;
    // 0 1 2
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    if(page == 1) {
        return;
    } else if (page == 0) {
        [self pageMoveToRight];
    } else {
        [self pageMoveToLeft];
    }

    CGPoint p = CGPointZero;
    p.x = pageWidth;
    [scrollView setContentOffset:p animated:NO];

依次向右或向左移动UIScrollView的subView方法:

- (void)pageMoveToRight {
    UIView *tmp = _pageSecond;
    _pageSecond = _pageFirst;
    _pageFirst = _pageThird;
    _pageThird = tmp;
    
    [self setPageFrame];
}

- (void)pageMoveToLeft {
    UIView *tmp = _pageSecond;
    _pageSecond = _pageThird;
    _pageThird = _pageFirst;
    _pageFirst = tmp;
    
    [self setPageFrame];
}



本文所有代码下载链接:http://download.csdn.net/detail/i2c_rs485/4394375,我使用xcode4.3.2


我参考过的资料:

http://www.cocoachina.com/bbs/read.php?tid=7013&page=2   cocoachina的讨论

http://www.dreamingwish.com/dream-2011/uiscrollview-infinite-loop-scrolling.html  我都是从这篇文章学习到的正确方法,强烈建议你们也看一下,感谢Seven's

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值