首页 > 移动平台 > 详细

iOS-创建UIScrollerView

时间:2016-08-19 13:00:11      阅读:253      评论:0      收藏:0      [点我收藏+]
-(void)createCell{
    scrollerArr = self.infoArray[0];
 //    创建
    _scrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, SCROLLERVIEW_HIGHT)];
    _scrollerView.contentSize = CGSizeMake(scrollerArr.count * SCREEN_SIZE.width, SCROLLERVIEW_HIGHT);
    _scrollerView.pagingEnabled = YES;
    _scrollerView.delegate = self;
    _scrollerView.bounces = NO;
    for (int i = 0; i <= scrollerArr.count; i++) {
         if (i == scrollerArr.count) {
            WJJScrollerModel * model = scrollerArr[0];
            WJJImageView * imageView = [[WJJImageView alloc] initWithFrame:CGRectMake(SCREEN_SIZE.width * scrollerArr.count, 0,SCREEN_SIZE.width, SCROLLERVIEW_HIGHT)];
            [imageView setImageWithURL:[NSURL URLWithString:model.app_picpath] placeholderImage:nil];
            [_scrollerView addSubview:imageView];
        }
        else
        {
            WJJScrollerModel * model = scrollerArr[i];
            WJJImageView * imageView = [[WJJImageView alloc] initWithFrame:CGRectMake(SCREEN_SIZE.width * i, 0, SCREEN_SIZE.width, SCROLLERVIEW_HIGHT)];
            [imageView setImageWithURL:[NSURL URLWithString:model.app_picpath] placeholderImage:nil];
            [_scrollerView addSubview:imageView];
            
        }
    }
    
    pageCon = [[UIPageControl alloc] initWithFrame:CGRectMake(SCREEN_SIZE.width - PAGCONTROLLER_WIDTH, SCROLLERVIEW_HIGHT - 20, PAGCONTROLLER_WIDTH, 20)];
    pageCon.numberOfPages = scrollerArr.count;
    pageCon.pageIndicatorTintColor = [UIColor whiteColor];
    pageCon.currentPageIndicatorTintColor = [UIColor redColor];
    pageCon.currentPage = 0;
   
    
    [self.contentView addSubview:_scrollerView];
     [self.contentView addSubview:pageCon];
}

#pragma mark - UIScollerView 代理

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int currentPage = (_scrollerView.contentOffset.x / SCREEN_SIZE.width) ;
    if (_scrollerView.contentOffset.x / SCREEN_SIZE.width == scrollerArr.count ) {
        pageCon.currentPage = 0;
[_scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
    }
    else
    {
        pageCon.currentPage = currentPage;
    }

}

#pragma mark - 定时器
-(void)addTimer
{
    _timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerGo) userInfo:nil repeats:YES];
}

-(void)timerGo
{
    if (_scrollerView.contentOffset.x / SCREEN_SIZE.width ==  scrollerArr.count ) {
        pageCon.currentPage = 0;
        [_scrollerView setContentOffset:CGPointMake(0, 0) animated:NO];
        
    }
    else
    {
        [_scrollerView setContentOffset:CGPointMake(_scrollerView.contentOffset.x + SCREEN_SIZE.width, 0) animated:YES];
    }

}

-(void)dealloc
{
    if (_timer != nil) {
        [_timer invalidate];
    }
}

 

iOS-创建UIScrollerView

原文:http://www.cnblogs.com/WJJ-Dream/p/5786979.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!