123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- //
- // 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.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{
- [_timer setFireDate:[NSDate distantFuture]];//关闭定时器
- }
- }
- #pragma mark- PageControl绑定ScrollView
- - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
-
- if ([scrollView isMemberOfClass:[UITableView class]]) {
-
- }else {
- int index = fabs(scrollView.contentOffset.x) / scrollView.frame.size.width; //当前是第几个视图
- _pageControl.currentPage = index;
- _imageNum.text = [NSString stringWithFormat:@"%d / %d",(int)_pageControl.currentPage+1,(int)_totalNum];
- }
- }
- - (void)setImgArray:(NSArray *)imgArray{
-
- _imgArray = imgArray;
- _totalNum = [imgArray count];
-
- for (int i = 0; i<_totalNum; 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];
- img.image = [UIImage imageNamed:imgArray[i]];
- [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页
- }
- //程序内跳转
- -(void)skipToADLink:(UITapGestureRecognizer *)gesture
- {
-
- }
- - (void)openTimer{
- [_timer setFireDate:[NSDate distantPast]];//开启定时器
- }
- - (void)closeTimer{
- [_timer setFireDate:[NSDate distantFuture]];//关闭定时器
- }
- @end
|