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

iOS 有关自动轮播图片

时间:2015-07-03 12:13:47      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

  //初始化当前视图
        _currentImageView = [[UIImageView alloc] init];
        [_currentImageView setImageWithURL:[NSURL URLWithString:[array_pic objectAtIndex:0]]];
        _currentImageView.frame = CGRectMake(myScroller.frame.size.width, 0, myScroller.frame.size.width,180);
//        _currentImageView.contentMode = UIViewContentModeScaleAspectFill;
        [myScroller addSubview:_currentImageView];
        
        _nextImageView = [[UIImageView alloc] init];
        [_nextImageView  setImageWithURL:[NSURL URLWithString:[array_pic objectAtIndex:1]]];
        _nextImageView.frame = CGRectMake(myScroller.frame.size.width * 2, 0,  myScroller.frame.size.width,180);
//        _nextImageView.contentMode = UIViewContentModeScaleAspectFill;
        [myScroller addSubview:_nextImageView];
        
        //初始化上一个视图
        _previousView = [[UIImageView alloc] init];
        [_previousView setImageWithURL:[NSURL URLWithString:[array_pic objectAtIndex:3]]];
        _previousView.frame = CGRectMake(0, 0,myScroller.frame.size.width,180);
//        _previousView.contentMode = UIViewContentModeScaleAspectFill;
       
        [myScroller addSubview:_previousView];
    
    }
   
    {
        SMPageControl *pageControl = [[SMPageControl alloc] init];
        [pageControl setFrame:CGRectMake(0, myScroller.frame.size.height+64 - 10.0f,myScroller.frame.size.width,10.0f)];
        [pageControl setNumberOfPages:4];
        [pageControl setBackgroundColor:[UIColor clearColor]];
        [pageControl setCurrentPage:0];
        [pageControl setCurrentPageIndicatorTintColor:[UIColor lightGrayColor]];
        [pageControl setPageIndicatorTintColor:[UIColor grayColor]];
        myPageControl=pageControl;
        [self.view addSubview:myPageControl];
    }


if (![myTimer isValid]) {
        myTimer=[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(updateAuto) userInfo:nil repeats:YES];
    }





#pragma mark 时钟动画调用方法
- (void)updateAuto
{
    if (_isDraging) {
        return;
    }
    
    CGPoint offset = myScroller.contentOffset;
    
    offset.x += self.view.frame.size.width;
    if (offset.x > self.view.frame.size.width*2) {
        offset.x = self.view.frame.size.width;
    }
    
    [myScroller setContentOffset:offset animated:YES];
}


#pragma mark - 代理方法
 #pragma mark 准备开始拖动
 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
 {
         _isDraging = YES;
     }

 #pragma mark 视图停止滚动
 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
 {
         _isDraging = NO;
}


#pragma mark 已经拖动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    static int i = 0;//当前展示的是第几张图片
   
    float offset = scrollView.contentOffset.x;
    if (_nextImageView.image == nil || _previousView.image == nil) {
        
        //加载下一个视图
        [_nextImageView setImageWithURL:[NSURL URLWithString:[array_pic objectAtIndex:i == kCount ? 0: i+1]]];
        
        //加载上一个视图
        [_previousView setImageWithURL:[NSURL URLWithString:[array_pic objectAtIndex:i == 0 ? kCount : i - 1]]];
    }
    
    if (offset == 0) {
        _currentImageView.image = _previousView.image;
        scrollView.contentOffset = CGPointMake(scrollView.bounds.size.width, 0);
        _previousView.image = nil;
        
        if (i == 0) {
            i = kCount;
        }else{
            i -= 1;
        }
        
    }
    
    if (offset == scrollView.bounds.size.width * 2) {
        _currentImageView.image = _nextImageView.image;
        scrollView.contentOffset = CGPointMake(scrollView.bounds.size.width, 0);
        _nextImageView.image = nil;
        
        if (i == kCount) {
            i = 0;  //如果当前图片索引在最后一个,则重置为0,否则+1
        }else{
            i += 1;
        }
    }
     [myPageControl setCurrentPage:i];
}

 

iOS 有关自动轮播图片

标签:

原文地址:http://www.cnblogs.com/niit-soft-518/p/4618120.html

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