博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UIScrollerView ,UIPageControl混搭使用,添加定时器,无限循环滚动
阅读量:4327 次
发布时间:2019-06-06

本文共 3326 字,大约阅读时间需要 11 分钟。

- (void)viewDidLoad {

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,240)];

    [self.view addSubview:view];

    //创建实例化UIScrollerView

    imageScrollerview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,[UIScreenmainScreen].bounds.size.width, 240)];

    imageScrollerview.contentSize = CGSizeMake(imageScrollerview.frame.size.width*6,0);   //必须设置的,滚动范围

    imageScrollerview.scrollEnabled = YES;   //是否支持滚动

    imageScrollerview.pagingEnabled = YES;  //整页滚动

    imageScrollerview.delegate = self;

    imageScrollerview.showsVerticalScrollIndicator = false;  //隐藏滑轮

    imageScrollerview.showsHorizontalScrollIndicator = false;

 

    [view addSubview:imageScrollerview];

    

    for (int i = 0; i<6; i++) {

        NSString *str = [NSString stringWithFormat:@"%d.jpg",i];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width*i, 0,[UIScreen mainScreen].bounds.size.width, imageScrollerview.frame.size.height)];

        imageView.image = [UIImageimageNamed:str];

        [imageScrollerview addSubview:imageView];

    }

    //设置起始位置 setContentOffset设置位置的,固定位置

    [imageScrollerview setContentOffset:CGPointMake([UIScreenmainScreen].bounds.size.width, 0)];

    //UIPageControl初始化

    pageControl = [[UIPageControlalloc] initWithFrame:CGRectMake([UIScreenmainScreen].bounds.size.width-100,imageScrollerview.frame.size.height-20, 100, 20)];

 pageCOntrol.currentPage = 0;   //设置当前页

    pageControl.numberOfPages = 4;

    [view addSubview:pageControl];

    

    //计时器

    NSTimer *time = [NSTimer scheduledTimerWithTimeInterval:3.0target:selfselector:@selector(updateTime:) userInfo:nilrepeats:YES];

    [[NSRunLoop currentRunLoop] addTimer:time forMode:NSDefaultRunLoopMode];

 

}

//计时器,3s一次

-(void) updateTime:(id) sender

{

    CGPoint point = imageScrollerview.contentOffset;

    if(point.x == [UIScreen mainScreen].bounds.size.width*4)

    {

        [imageScrollerview setContentOffset:CGPointMake(0, 0) animated:NO];

         imageScrollerview.frame = CGRectMake(0, 0, [UIScreenmainScreen].bounds.size.width, 240); //防止位置跳动

        

         [imageScrollerviewscrollRectToVisible:CGRectMake([UIScreenmainScreen].bounds.size.width, 0, [UIScreenmainScreen].bounds.size.width, 240) animated:YES];

        pageControl.currentPage = imageScrollerview.contentOffset.x/[UIScreenmainScreen].bounds.size.width;

    }

    else

    {

        [imageScrollerview scrollRectToVisible:CGRectMake(point.x + [UIScreenmainScreen].bounds.size.width, 0, [UIScreenmainScreen].bounds.size.width, 240) animated:YES];  //移动设置

        pageControl.currentPage = imageScrollerview.contentOffset.x/[UIScreen mainScreen].bounds.size.width;

    }

}

#pragma mark UIScrollerViewDelegate

-(void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    if (imageScrollerview.contentOffset.x == 0)

    {

        [imageScrollerview setContentOffset:CGPointMake([UIScreenmainScreen].bounds.size.width*4, 0) animated:NO];

        imageScrollerview.frame = CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 240);//防止位置跳动

        

    }

    elseif (imageScrollerview.contentOffset.x == [UIScreen mainScreen].bounds.size.width*5)

    {

        [imageScrollerview setContentOffset:CGPointMake([UIScreen mainScreen].bounds.size.width, 0) animated:NO];

        imageScrollerview.frame = CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 240);//防止位置跳动

    }

    

    pageControl.currentPage = imageScrollerview.contentOffset.x/[UIScreen mainScreen].bounds.size.width - 1;

}

转载于:https://www.cnblogs.com/Pegboggs/p/5062467.html

你可能感兴趣的文章
游戏开发的调试机制
查看>>
js中深拷贝代码实现
查看>>
远程获得乐趣的 Linux 命令
查看>>
Celery Flower监控,完美搞定
查看>>
完美分页
查看>>
IE8/9 JQuery.Ajax 上传文件无效
查看>>
Uploadify--上传图片
查看>>
B. Inna and Nine
查看>>
建议性列表输入文本框
查看>>
RTSP 资料
查看>>
[转]uboot中SPL作用
查看>>
Excel VBA Range对象基本操作应用示例
查看>>
html5拖拽
查看>>
kerboros安装
查看>>
我的学习之路_第二十九章_bootstrap
查看>>
Python读取文件行数不对
查看>>
考研经验交流
查看>>
手游助手应用源码项目
查看>>
职场心得笔记
查看>>
Android context(Application/Activity)与内存泄露
查看>>