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. 用c语言编写直接插入法

    #include<stdio.h> //直接插入法 void D_insert(int s[],int n); int main() { int i; ]; printf("pl ...

  2. junit基础篇、中级篇-实例代码

    学习文章: http://blog.csdn.net/andycpp/article/details/1327147 http://wenku.baidu.com/link?url=C27gDEj0l ...

  3. PDF 补丁丁 0.4.2.1013 测试版发布:修复替换PDF文档字体等问题

    新的测试版修复了若干问题,建议下载了旧测试版的用户更新.修复的问题列表: 替换PDF文档字体可能将字体宽度变成0. 书签编辑器在加载XML书签时不显示对应文档的预览. 自动生成书签功能复制文本筛选器后 ...

  4. git中Member设置为Developer也可以的

    之前说Member至少要设置为master,或者owner才行,昨天发现依然可以设置为developer. 因为有个位置可以设置Developers can push,√上就可以了. 比如,我不希望大 ...

  5. PHP投票系统

    1.投票页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...

  6. OpenCV源码分析:RGB到其他色彩空间的转换

    1.流程调用图 2.部分代码分析 //模板函数进行颜色空间的转换 template <typename Cvt> void CvtColorLoop(const Mat& src, ...

  7. ionic 安装遇到的问题以及解决方案

    公司里要用到 Ionic 做移动App 混合开发 一个环境搭建折腾了好几天.一是公司权限问题,二是网络问题,你懂得. Ionic 环境搭建官网有教程.本来几行命令就能搞定的事,一旦遇到网络问题,就蛋疼 ...

  8. cocopods 知识集合 及 一个 好的 国外iOS技术翻译站

    http://www.exiatian.com/cocoapods%E5%AE%89%E8%A3%85%E4%BD%BF%E7%94%A8%E5%8F%8A%E9%85%8D%E7%BD%AE%E7% ...

  9. 计算机网络(5)-----ICMP协议和PING程序

    控制报文协议(Internet Control Message Protocol) 定义 它是TCP/IP协议族的一个子协议,用于在IP主机.路由器之间传递控制消息.控制消息是指网络通不通.主机是否可 ...

  10. Setup Spark source code environment

    1. Install Java and set JAVA_HOME 2. Install Eclipse Juno Java IDE, Scala plugin and Scala Test 3. D ...