123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- //
- // 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<adArray.count; i++) {
-
- UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(i*CGRectGetWidth(_scrollView.frame), 0, CGRectGetWidth(_scrollView.frame), CGRectGetHeight(_scrollView.frame))];
- img.contentMode = UIViewContentModeScaleAspectFill;
- [img setClipsToBounds:YES];
- img.image = [UIImage imageNamed:adArray[i]];
- [_scrollView addSubview:img];
- }
- _imageNum.text = [NSString stringWithFormat:@"%d / %d",(int)_pageControl.currentPage+1,(int)adArray.count];
- _pageControl.numberOfPages = adArray.count; //设置页数 //滚动范围 600=300*2,分2页
- CGRect frame;
- frame = _pageControl.frame;
- frame.size.width = 15*adArray.count;
- _pageControl.frame = frame;
-
- _scrollView.contentSize = CGSizeMake(CGRectGetWidth(_scrollView.frame)*3,CGRectGetHeight(_scrollView.frame));//滚动范围 600=300*2,分2页
- }
- }
- //程序内跳转
- -(void)skipToADLink:(UITapGestureRecognizer *)gesture
- {
- NSInteger index = gesture.view.tag;
-
- NSDictionary *dic = _imgArray[index];
-
- NSString *skipType = dic[@"OPENTYPE"];
-
- if ([skipType isEqualToString:@"1"]) {
-
- ADLinkVC *ad = [[ADLinkVC alloc] init];
- ad.title = dic[@"TITLE"];
- ad.urlString = dic[@"URL"];
- if (ad.urlString && ad.urlString.length != 0) {
- [_supVC navPushHideTabbarToVC:ad];
- }
-
- }else{
-
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:dic[@"URL"]]];
- }
- }
- - (void)openTimer{
- [_timer setFireDate:[NSDate distantPast]];//开启定时器
- }
- - (void)closeTimer{
- [_timer setFireDate:[NSDate distantFuture]];//关闭定时器
- }
- @end
|