首页 > 其他 > 详细

UIScroView 3倍的contentSize,左右Scroll时,懒惰加载View

时间:2014-03-12 05:33:50      阅读:428      评论:0      收藏:0      [点我收藏+]

UIScroView 3倍的contentSize,左右Scroll时,懒惰添加左右的View

用途:分段加载数据

 

定义枚举:

bubuko.com,布布扣
typedefenum {

    ViewPositionLeft = 101,

    ViewPositionCenter,

    ViewPositionRight

} ViewPosition;
bubuko.com,布布扣

 

变量定义:

bubuko.com,布布扣
    NSInteger _currnetPage;

    CGRect _leftFrame;

    CGRect _centerFrame;

    CGRect _rightFrame;

    UIScrollView *_sv;
bubuko.com,布布扣

 

初始化变量:

bubuko.com,布布扣
-(void)createView

{

 

    _currnetPage = 0;

    

    _sv = [[UIScrollView alloc] init];

    _sv.frame = CGRectMake(0, 0, 320, 424);

    _sv.pagingEnabled = YES;

    _sv.backgroundColor = [UIColorwhiteColor];

    _sv.contentSize = CGSizeMake(_sv.frame.size.width*3, _sv.frame.size.height);

    

    _sv.showsHorizontalScrollIndicator = YES;

    _sv.showsVerticalScrollIndicator = NO;

 

    

    for (int i = 2; i > -1; i--) {

        CGRect frame;

        frame.origin.x = _sv.frame.size.width * (2 - i);

        frame.origin.y = 0;

        frame.size = _sv.frame.size;

        

        UIView *lbl = [selfcreateLabeViewWithCurrentPage:i];

        lbl.frame = frame;

        

        if (i == 2) {

            _leftFrame = frame;

            lbl.tag = ViewPositionLeft;

            

        }

        if (i == 1) {

            _centerFrame = frame;

            lbl.tag = ViewPositionCenter;

        }

        if (i == 0) {

            _rightFrame = frame;

            lbl.tag = ViewPositionRight;

        }

        

        

        [_sv addSubview:lbl];

        

        

    }

    _sv.delegate = self;            

    

    [_svscrollRectToVisible:_rightFrameanimated:NO];

    

    [self.view addSubview:_sv];

}
bubuko.com,布布扣

 

创建新View的方法(selfcreateLabeViewWithCurrentPage):

bubuko.com,布布扣
- (UIView *)createLabeViewWithCurrentPage:(int)Page

{

    

    UILabel *textView = [[UILabel alloc] init];

    textView.text = [NSString stringWithFormat:@"Page %d",Page];

    textView.font = [UIFont systemFontOfSize:38];

    textView.textColor = [UIColor blackColor];

    textView.backgroundColor = [UIColorwhiteColor];

    textView.textAlignment = UITextAlignmentCenter;

 

    [_sv addSubview:textView];

    return textView;

}

UIScrollViewDelegate方法:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)sv

{

    

    

    BOOL towardsToLeft = NO;

    CGFloat width = _sv.frame.size.width;

    

    if (_sv.contentOffset.x == width) {

        if (_currnetPage == 0) {

            _currnetPage++;

        }

        

        return;

    }

    if (_sv.contentOffset.x > width) {

        towardsToLeft = YES;

        if(_currnetPage == 0){

//            NSLog(@"currentPage = 0;  向左");

            return;

        }

//        NSLog(@"向左");

    }

    else{

//        NSLog(@"向右");

    }

    

    

//    _previousPage = _currnetPage;

    if (towardsToLeft == YES) {

        _currnetPage--;

    }

    else{

        _currnetPage++;

    }

    

    

    if (_currnetPage == 0) {

        

        

    }

    else  {

        [sv scrollRectToVisible:_centerFrameanimated:NO];

        UIView *view = [_sv viewWithTag:_currnetPage];

        view.frame = _centerFrame;

        

        if (towardsToLeft == YES) {

            UIView *viewRLeft = [_sv viewWithTag:ViewPositionLeft];

            [viewRLeft removeFromSuperview];

            

            UIView *viewRCenter = [_sv viewWithTag:ViewPositionCenter];


            viewRCenter.frame = _leftFrame;

            viewRCenter.tag = ViewPositionLeft;

            

            UIView *viewRRight = [_sv viewWithTag:ViewPositionRight];

            viewRRight.frame = _centerFrame;

            viewRRight.tag = ViewPositionCenter;

            

            UIView *viewRight = [self createLabeViewWithCurrentPage:_currnetPage - 1];

            viewRight.frame = _rightFrame;

            viewRight.tag = ViewPositionRight;

            [sv addSubview:viewRight];

        }

        else{

            UIView *viewRRight = [_sv viewWithTag:ViewPositionRight];

            [viewRRight removeFromSuperview];

            

            UIView *viewRCenter = [_sv viewWithTag:ViewPositionCenter];

            viewRCenter.frame = _rightFrame;

            viewRCenter.tag = ViewPositionRight;

            

            UIView *viewRLeft = [_sv viewWithTag:ViewPositionLeft];

            viewRLeft.frame = _centerFrame;

            viewRLeft.tag = ViewPositionCenter;

            

            UIView *viewLeft = [self createLabeViewWithCurrentPage:_currnetPage + 1];

            viewLeft.frame = _leftFrame;

            viewLeft.tag = ViewPositionLeft;

            [sv addSubview:viewLeft];

            

            

            

        }

    }

 

}

 
bubuko.com,布布扣

 

 希望对用到的朋友有用。

UIScroView 3倍的contentSize,左右Scroll时,懒惰加载View,布布扣,bubuko.com

UIScroView 3倍的contentSize,左右Scroll时,懒惰加载View

原文:http://www.cnblogs.com/fphuang/p/3594322.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!