// // AdvertisingColumn.m // CustomTabBar // // Created by shikee_app05 on 14-12-30. // Copyright (c) 2014年 chan kaching. All rights reserved. // #import "AdvertisingColumn.h" #import "ADLinkVC.h" @implementation AdvertisingColumn - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { _scrollView = [[UIScrollView alloc]initWithFrame:frame]; _scrollView.delegate = self;//设置代理UIscrollViewDelegate _scrollView.showsVerticalScrollIndicator = NO;//是否显示竖向滚动条 _scrollView.showsHorizontalScrollIndicator = NO;//是否显示横向滚动条 _scrollView.pagingEnabled = YES;//是否设置分页 _scrollView.bounces = NO; [self addSubview:_scrollView]; /* ***容器,装载 */ UIView *containerView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.frame)-20, CGRectGetWidth(self.frame), 20)]; containerView.backgroundColor = [UIColor clearColor]; [self addSubview:containerView]; UIView *alphaView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(containerView.frame), CGRectGetHeight(containerView.frame))]; alphaView.alpha = 0.7; [containerView addSubview:alphaView]; //分页控制 _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(kSize.width*0.5 - 15, 0, kSize.width, 20)]; _pageControl.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; _pageControl.pageIndicatorTintColor = [UIColor lightGrayColor]; _pageControl.currentPage = 0; //初始页码为0 [containerView addSubview:_pageControl]; //图片张数 _imageNum = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, CGRectGetWidth(containerView.frame)-20, 20)]; _imageNum.font = [UIFont scaleSize:15]; _imageNum.backgroundColor = [UIColor clearColor]; _imageNum.textColor = [UIColor whiteColor]; _imageNum.textAlignment = NSTextAlignmentRight; // [containerView addSubview:_imageNum]; /* ***配置定时器,自动滚动广告栏 */ _timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode]; //[_timer setFireDate:[NSDate distantFuture]];//关闭定时器 } self.backgroundColor = [UIColor orangeColor]; return self; } //--------------------------------------------------------------------------------- -(void)timerAction:(NSTimer *)timer{ if (_totalNum>1) { CGPoint newOffset = _scrollView.contentOffset; newOffset.x = newOffset.x + CGRectGetWidth(_scrollView.frame); // NSLog(@"newOffset.x = %f",newOffset.x); if (newOffset.x > (CGRectGetWidth(_scrollView.frame) * (_totalNum-1))) { newOffset.x = 0 ; } int index = newOffset.x / CGRectGetWidth(_scrollView.frame); //当前是第几个视图 newOffset.x = index * CGRectGetWidth(_scrollView.frame); _imageNum.text = [NSString stringWithFormat:@"%d / %d",index+1,(int)_totalNum]; [_scrollView setContentOffset:newOffset animated:YES]; }else{ CGPoint newOffset = _scrollView.contentOffset; newOffset.x = newOffset.x + CGRectGetWidth(_scrollView.frame); // NSLog(@"newOffset.x = %f",newOffset.x); if (newOffset.x > (CGRectGetWidth(_scrollView.frame) * (2))) { newOffset.x = 0 ; } int index = newOffset.x / CGRectGetWidth(_scrollView.frame); //当前是第几个视图 newOffset.x = index * CGRectGetWidth(_scrollView.frame); _imageNum.text = [NSString stringWithFormat:@"%d / %d",index+1,3]; [_scrollView setContentOffset:newOffset animated:YES]; //[_timer setFireDate:[NSDate distantFuture]];//关闭定时器 } } #pragma mark- PageControl绑定ScrollView - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ //滚动就执行--手动滑动后,定时器要重新计时dansonmark if ([scrollView isMemberOfClass:[UITableView class]]) { }else { int index = fabs(scrollView.contentOffset.x) / scrollView.frame.size.width; //当前是第几个视图 _pageControl.currentPage = index; for (UIView *view in scrollView.subviews) { if(view.tag == index){ }else{ } } } } - (void)setImgArray:(NSArray *)imgArray{ _imgArray = imgArray; _totalNum = [imgArray count]; //NSLog(@"广告位图片imgArray---->%@",imgArray); if (_totalNum > 0) { for (int i = 0; i<_totalNum; i++) { NSDictionary *dic = imgArray[i]; UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(i*CGRectGetWidth(_scrollView.frame), 0, CGRectGetWidth(_scrollView.frame), CGRectGetHeight(_scrollView.frame))]; img.contentMode = UIViewContentModeScaleAspectFill; //img.image = [UIImage imageNamed:imgArray[i]]; [img setClipsToBounds:YES]; NSString *path = dic[@"IMG"]; if (path && ![path hasPrefix:@"http"]){ path = [imgPreFix stringByAppendingString:path]; } [img sd_setImageWithURL:[NSURL URLWithString:path] placeholderImage:[UIImage imageNamed:@"subject_main_banner.jpg"]]; [img setTag:i]; //把手势添加到指定视图上 [img setUserInteractionEnabled:YES]; UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(skipToADLink:)]; [img addGestureRecognizer:tapGesture]; [_scrollView addSubview:img]; } _imageNum.text = [NSString stringWithFormat:@"%d / %d",(int)_pageControl.currentPage+1,(int)_totalNum]; _pageControl.numberOfPages = _totalNum; //设置页数 //滚动范围 600=300*2,分2页 CGRect frame; frame = _pageControl.frame; frame.size.width = 15*_totalNum; _pageControl.frame = frame; _scrollView.contentSize = CGSizeMake(CGRectGetWidth(_scrollView.frame)*_totalNum,CGRectGetHeight(_scrollView.frame));//滚动范围 600=300*2,分2页 }else{ // UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_scrollView.frame), CGRectGetHeight(_scrollView.frame))]; // [img setImage:[UIImage imageNamed:@"subject_main_banner.jpg"]]; // img.userInteractionEnabled = YES; // [_scrollView addSubview:img]; // //_imageNum.text = @"提示:滚动栏无数据。"; // // _scrollView.contentSize = CGSizeMake(CGRectGetWidth(_scrollView.frame),CGRectGetHeight(_scrollView.frame)); NSArray *adArray = @[@"subject_main_banner.jpg",@"subject_main_banner_2.jpg",@"subject_main_banner_3.jpg"]; for (int i = 0; i