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

用途:分段加载数据

定义枚举:

typedefenum {

    ViewPositionLeft = ,

    ViewPositionCenter,

    ViewPositionRight

} ViewPosition;

变量定义:

    NSInteger _currnetPage;

    CGRect _leftFrame;

    CGRect _centerFrame;

    CGRect _rightFrame;

    UIScrollView *_sv;

初始化变量:

-(void)createView

{

    _currnetPage = ;

    _sv = [[UIScrollView alloc] init];

    _sv.frame = CGRectMake(, , , );

    _sv.pagingEnabled = YES;

    _sv.backgroundColor = [UIColorwhiteColor];

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

    _sv.showsHorizontalScrollIndicator = YES;

    _sv.showsVerticalScrollIndicator = NO;

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

        CGRect frame;

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

        frame.origin.y = ;

        frame.size = _sv.frame.size;

        UIView *lbl = [selfcreateLabeViewWithCurrentPage:i];

        lbl.frame = frame;

        if (i == ) {

            _leftFrame = frame;

            lbl.tag = ViewPositionLeft;

        }

        if (i == ) {

            _centerFrame = frame;

            lbl.tag = ViewPositionCenter;

        }

        if (i == ) {

            _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:];

    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 == ) {

            _currnetPage++;

        }

        return;

    }

    if (_sv.contentOffset.x > width) {

        towardsToLeft = YES;

        if(_currnetPage == ){

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

            return;

        }

//        NSLog(@"向左");

    }

    else{

//        NSLog(@"向右");

    }

//    _previousPage = _currnetPage;

    if (towardsToLeft == YES) {

        _currnetPage--;

    }

    else{

        _currnetPage++;

    }

    if (_currnetPage == ) {

    }

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

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

            viewLeft.frame = _leftFrame;

            viewLeft.tag = ViewPositionLeft;

            [sv addSubview:viewLeft];

        }

    }

}

希望对用到的朋友有用。

UIScroView 3倍的contentSize,左右Scroll时,懒惰加载View的更多相关文章

  1. mui scroll和上拉加载/下拉刷新

    mui中 scroll和上拉加载/下拉刷新同时存在会出现两个滚动条 把/*   */ /* //mui页面鼠标拖动代码: mui('.mui-scroll-wrapper').scroll({ dec ...

  2. js 鼠标滚动到某屏时,加载那一屏的数据,仿京东首页楼层异步加载模式

    js用处:在做商城时,首页图片太多,严重影响首页打开速度,所以我们需要用到异步加载楼层.js名称:鼠标滚动到某屏时,加载那一屏的数据,仿京东首页楼层模式js解释:1.用于商城的楼层内容异步加载,滚动条 ...

  3. 基于JQuery实现滚动到页面底端时自动加载更多信息

    基于JQuery实现滚动到页面底端时自动加载更多信息 关键代码: 代码如下: var stop=true; $(window).scroll(function(){ totalheight = par ...

  4. jQuery实现滚动时动态加载页面内容

    有些网站的网页内容不是一次性加载完毕的,而是在鼠标向下滚动时动态加载的,这是怎么做到的呢?看下面代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...

  5. Tomcat启动时自动加载Servlet

    1.想做一个服务启动时自动启动一不停止的获取订阅功能 2.之前是做一个Jsp页面请求servlet来触发方法 3.现在实现Tomcat启动时自动加载Servlet 1.Tomcat中启动Servlet ...

  6. 如何让openvpn在windows启动时自动加载

    在非常需要vpn的人群中,他们几乎一整天都连接着vpn,但是每次开机都要连接vpn的过程有时候比较繁琐对于新手而言.这篇文章主要是说明如何在windows启动时自动加载openvpn自动连接,该教程适 ...

  7. selenium启动Chrome时,加载用户配置文件

    selenium启动Chrome时,加载用户配置文件   Selenium操作浏览器是不加载任何配置的,网上找了半天,关于Firefox加载配置的多点,Chrome资料很少,下面是关于加载Chrome ...

  8. jquery mobile 请求数据方法执行时显示加载中提示框

    在jquery mobile开发中,经常需要调用ajax方法,异步获取数据,如果异步获取数据方法由于网速等等的原因,会有一个反应时间,如果能在点击按钮后数据处理期间,给一个正在加载的提示,客户体验会更 ...

  9. 测试img在不显示时是否加载?

    一直搞不明白,隐藏的元素的背景图,在页面加载时,是否自动加载? img隐藏时,图片会加载吗? 测试代码如下: <!DOCTYPE html> <html> <head&g ...

随机推荐

  1. Android Hook Dexposed原理小析

    dexposed是阿里巴巴在xposed框架上面开发的hotpatch一套框架 当然hotpatch的方式有很多,这里先介绍下dexposed原理 Demo中有个test函数, 在调用hook之前正常 ...

  2. jquery获取(设置)节点的属性与属性值

    1. attr(属性名)        //获取属性的值(取得第一个匹配元素的属性值.通过这个方法可以方便地从第一个匹配元素中获取一个属性的值.如果元素没有相应属性,则返回 undefined ) 2 ...

  3. &与&&的区别

    &是“逻辑与”(“按位与”“位运算符”),一定要判断完所有的条件才能确定到底返回true还是false. &&是“短路与”(“逻辑运算符”),当从左至右判断时,一旦出现有一个条 ...

  4. Mac 搭建 SVN 服务器环境

    Mac 搭建 SVN 服务器环境 一.创建svn repository svnadmin create /path/svn/pro //仓库位置,svn是svn的目录,pro是一个版本库的目录 PS: ...

  5. web 标准相关

    摘自:http://www.cnblogs.com/artech/p/restful-web-api-01.html 一.TCP/IP与HTTP TCP/IP是以IP和TCP协议为核心的一整套网络协议 ...

  6. CSS3判断手机横屏竖屏

    原理: 当用户旋转屏幕的时候,会进入到你的监听方法中,然后通过window.orientation来获取当前屏幕的状态:0 - 竖屏90 - 逆时针旋转横屏-90 - 顺时针旋转横屏180 - 竖屏, ...

  7. JS判断form内所有表单是否为空

    function checkForm(){ var input_cart=document.getElementsByTagName("INPUT"); for(var   i=0 ...

  8. cmake gcc等安装备案

    cmake安装,参照 http://www.cnblogs.com/voyagflyer/p/5323748.html cmake2.8以上 安装后的是/usr/local/bin/cmake -ve ...

  9. linux 下mysql的启动 、调试、排错

    Linux 下 MySQL 启动与关闭 说明 一.启动 1.1  MySQL 进程 可以用ps 命令查看进程: [root@rac2 ~]# ps -ef|grep mysql root     21 ...

  10. Android Camera进行拍照

    Android应用提供了Camera来控制拍照,使用Camera进行拍照的步骤: 1.调用Camera的open()方法打开相机. 2.调用Camera的getParameters()方法获取拍照参数 ...