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. centos6.5安装sublime text 2

    今天在看ueillemmx的博客的时候,看到一神级编辑器,随即安装试了试,我了个去,果然好用,自动补全,自动对齐,样样精通啊! 下面是根据ueillemmx的步骤在CentOS上安装Sublime的过 ...

  2. MySQL SQL

    SQL语句错误: Column count doesn't match value count at row 1 列计数与第1行的值计数不匹配 You have an error in your SQ ...

  3. OpenAl编程入门:播放一段音频

    OpenAl编程入门 关于OpenAl我就不多介绍了,这两篇说明对于初步了解已经足够了:http://baike.baidu.com/view/1355367.htmhttp://en.wikiped ...

  4. Canvas 画布小案例

    <script> window.onload = function () { draw('canvas1'); draw('canvas2'); draw('canvas3'); draw ...

  5. Linux diable ipv6

    在RHEL 5下面测试成功     linux下面禁止ipv6的方法: 来自: 杨志刚 博客 (http://yangzhigang.cublog.cn)   这里我所做的是Redhat Linux, ...

  6. MySQL 入门知识点

    参考网址:http://www.cnblogs.com/mr-wid/archive/2013/05/09/3068229.html 1.数值类数据列类型 数据列类型 存储空间 描述 TINYINT ...

  7. Josephu--Java链表实现

    public class Josephu { public static void main(String[] args) { Cyclink cyclink=new Cyclink(); cycli ...

  8. js获取页面宽度高度及屏幕分辨率

    网页可见区域宽:document.body.clientWidth 网页可见区域高:document.body.clientHeight 网页可见区域宽:document.body.offsetWid ...

  9. Android通过HttpURLConnection链接到网络,并获取网络数据

    1.判断网络是否连接 private void networkIsconnected(String str){ ConnectivityManager connMgr = (ConnectivityM ...

  10. Android 自动生成表格

    Layout.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...