码迷,mamicode.com
首页 > 其他好文 > 详细

UIScrollerView ,UIPageControl混搭使用,添加定时器,无限循环滚动

时间:2015-12-21 10:27:29      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:

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

}

UIScrollerView ,UIPageControl混搭使用,添加定时器,无限循环滚动

标签:

原文地址:http://www.cnblogs.com/Pegboggs/p/5062467.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!