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

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

用途:分段加载数据

 

定义枚举:

typedefenum {

    ViewPositionLeft = 101,

    ViewPositionCenter,

    ViewPositionRight

} ViewPosition;

 

变量定义:

    NSInteger _currnetPage;

    CGRect _leftFrame;

    CGRect _centerFrame;

    CGRect _rightFrame;

    UIScrollView *_sv;

 

初始化变量:

-(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];

}

 

创建新View的方法(selfcreateLabeViewWithCurrentPage):

- (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];

            

            

            

        }

    }

 

}

 

 

 希望对用到的朋友有用。

posted @ 2014-03-11 16:43  NOIP/NOI辅导  Views(1052)  Comments(0Edit  收藏  举报