码迷,mamicode.com
首页 > 移动开发 > 详细

ios开发之 -- 自动轮播图创建

时间:2017-09-02 19:04:56      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:ini   简单   ble   ios开发   开始   效果图   方法   val   color   

这里是oc版本的,简单记录下:

具体代码如下:

1,准备

#define FRAME [[UIScreen mainScreen] bounds]
#define WIDTH  FRAME.size.width
#define HEIGHT FRAME.size.height

2,具体实现

 //scrollview的添加
    _bigScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, WIDTH, HEIGHT)];
    _bigScrollView.showsHorizontalScrollIndicator = NO;
    _bigScrollView.contentSize = CGSizeMake(WIDTH *6, 0);
    _bigScrollView.pagingEnabled = YES;
    _bigScrollView.bounces = NO;
    _bigScrollView.delegate = self;
    [self.view addSubview:_bigScrollView];
    
    //图片内容的添加
    for ( int i = 0; i<6; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i *WIDTH, 0, WIDTH, HEIGHT)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i]];
        [_bigScrollView addSubview:imageView];
    }
    
    //pagecontrol的创建
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 40)] ;
    _pageControl.center = CGPointMake(_bigScrollView.center.x, _bigScrollView.center.y);
    _pageControl.numberOfPages = 6;
    _pageControl.currentPage = 0;
    _pageControl.pageIndicatorTintColor = [UIColor blueColor];
    _pageControl.currentPageIndicatorTintColor =[UIColor redColor];
    [self.view addSubview:_pageControl];
    [_pageControl addTarget:self action:@selector(pageControllerClick) forControlEvents:UIControlEventValueChanged];
    
    //定时器的创建
    _timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];

3,响应方法的实现

-(void)pageControllerClick
{
    [_bigScrollView setContentOffset:CGPointMake(_pageControl.currentPage*320, 0) animated:YES];
}

//在scrollview开始手动滑动的时候
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [_timer setFireDate:[NSDate distantFuture]];
}

//在scrollview添加一个延迟方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    _pageControl.currentPage = _bigScrollView.contentOffset.x/WIDTH;
    [self performSelector:@selector(disPast) withObject:nil afterDelay:2];//延迟方法
    
}

//启动定时器
-(void)disPast
{
    [_timer setFireDate:[NSDate distantPast]];
}

static int count=1;

//定时器的具体响应方法
-(void)onTimer
{
    _pageControl.currentPage+=count;
    if (_pageControl.currentPage==_pageControl.numberOfPages-1||_pageControl.currentPage==0)
    {
        count=-count;
    };
    [_bigScrollView setContentOffset:CGPointMake(_pageControl.currentPage*WIDTH, 0) animated:YES];
    
}

效果图就不加了,这里仅做记录用!

ios开发之 -- 自动轮播图创建

标签:ini   简单   ble   ios开发   开始   效果图   方法   val   color   

原文地址:http://www.cnblogs.com/hero11223/p/7467255.html

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