AdvertisingColumn.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // AdvertisingColumn.m
  3. // CustomTabBar
  4. //
  5. // Created by shikee_app05 on 14-12-30.
  6. // Copyright (c) 2014年 chan kaching. All rights reserved.
  7. //
  8. #import "AdvertisingColumn.h"
  9. #import "ADLinkVC.h"
  10. @implementation AdvertisingColumn
  11. - (id)initWithFrame:(CGRect)frame
  12. {
  13. self = [super initWithFrame:frame];
  14. if (self) {
  15. _scrollView = [[UIScrollView alloc]initWithFrame:frame];
  16. _scrollView.delegate = self;//设置代理UIscrollViewDelegate
  17. _scrollView.showsVerticalScrollIndicator = NO;//是否显示竖向滚动条
  18. _scrollView.showsHorizontalScrollIndicator = NO;//是否显示横向滚动条
  19. _scrollView.pagingEnabled = YES;//是否设置分页
  20. _scrollView.bounces = NO;
  21. [self addSubview:_scrollView];
  22. /*
  23. ***容器,装载
  24. */
  25. UIView *containerView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.frame)-20, CGRectGetWidth(self.frame), 20)];
  26. containerView.backgroundColor = [UIColor clearColor];
  27. [self addSubview:containerView];
  28. UIView *alphaView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(containerView.frame), CGRectGetHeight(containerView.frame))];
  29. alphaView.alpha = 0.7;
  30. [containerView addSubview:alphaView];
  31. //分页控制
  32. _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(kSize.width*0.5 - 15, 0, kSize.width, 20)];
  33. _pageControl.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  34. _pageControl.currentPage = 0; //初始页码为0
  35. [containerView addSubview:_pageControl];
  36. //图片张数
  37. _imageNum = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, CGRectGetWidth(containerView.frame)-20, 20)];
  38. _imageNum.font = [UIFont scaleSize:15];
  39. _imageNum.backgroundColor = [UIColor clearColor];
  40. _imageNum.textColor = [UIColor whiteColor];
  41. _imageNum.textAlignment = NSTextAlignmentRight;
  42. [containerView addSubview:_imageNum];
  43. /*
  44. ***配置定时器,自动滚动广告栏
  45. */
  46. _timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
  47. [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
  48. //[_timer setFireDate:[NSDate distantFuture]];//关闭定时器
  49. }
  50. self.backgroundColor = [UIColor orangeColor];
  51. return self;
  52. }
  53. //---------------------------------------------------------------------------------
  54. -(void)timerAction:(NSTimer *)timer{
  55. if (_totalNum>1) {
  56. CGPoint newOffset = _scrollView.contentOffset;
  57. newOffset.x = newOffset.x + CGRectGetWidth(_scrollView.frame);
  58. // NSLog(@"newOffset.x = %f",newOffset.x);
  59. if (newOffset.x > (CGRectGetWidth(_scrollView.frame) * (_totalNum-1))) {
  60. newOffset.x = 0 ;
  61. }
  62. int index = newOffset.x / CGRectGetWidth(_scrollView.frame); //当前是第几个视图
  63. newOffset.x = index * CGRectGetWidth(_scrollView.frame);
  64. _imageNum.text = [NSString stringWithFormat:@"%d / %d",index+1,(int)_totalNum];
  65. [_scrollView setContentOffset:newOffset animated:YES];
  66. }else{
  67. [_timer setFireDate:[NSDate distantFuture]];//关闭定时器
  68. }
  69. }
  70. #pragma mark- PageControl绑定ScrollView
  71. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  72. if ([scrollView isMemberOfClass:[UITableView class]]) {
  73. }else {
  74. int index = fabs(scrollView.contentOffset.x) / scrollView.frame.size.width; //当前是第几个视图
  75. _pageControl.currentPage = index;
  76. _imageNum.text = [NSString stringWithFormat:@"%d / %d",(int)_pageControl.currentPage+1,(int)_totalNum];
  77. }
  78. }
  79. - (void)setImgArray:(NSArray *)imgArray{
  80. _imgArray = imgArray;
  81. _totalNum = [imgArray count];
  82. for (int i = 0; i<_totalNum; i++) {
  83. UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(i*CGRectGetWidth(_scrollView.frame), 0, CGRectGetWidth(_scrollView.frame), CGRectGetHeight(_scrollView.frame))];
  84. img.contentMode = UIViewContentModeScaleAspectFill;
  85. //img.image = [UIImage imageNamed:imgArray[i]];
  86. [img setClipsToBounds:YES];
  87. img.image = [UIImage imageNamed:imgArray[i]];
  88. [img setTag:i];
  89. //把手势添加到指定视图上
  90. [img setUserInteractionEnabled:YES];
  91. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(skipToADLink:)];
  92. [img addGestureRecognizer:tapGesture];
  93. [_scrollView addSubview:img];
  94. }
  95. _imageNum.text = [NSString stringWithFormat:@"%d / %d",(int)_pageControl.currentPage+1,(int)_totalNum];
  96. _pageControl.numberOfPages = _totalNum; //设置页数 //滚动范围 600=300*2,分2页
  97. CGRect frame;
  98. frame = _pageControl.frame;
  99. frame.size.width = 15*_totalNum;
  100. _pageControl.frame = frame;
  101. _scrollView.contentSize = CGSizeMake(CGRectGetWidth(_scrollView.frame)*_totalNum,CGRectGetHeight(_scrollView.frame));//滚动范围 600=300*2,分2页
  102. }
  103. //程序内跳转
  104. -(void)skipToADLink:(UITapGestureRecognizer *)gesture
  105. {
  106. }
  107. - (void)openTimer{
  108. [_timer setFireDate:[NSDate distantPast]];//开启定时器
  109. }
  110. - (void)closeTimer{
  111. [_timer setFireDate:[NSDate distantFuture]];//关闭定时器
  112. }
  113. @end