AdvertisingColumn.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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.pageIndicatorTintColor = [UIColor lightGrayColor];
  35. _pageControl.currentPage = 0; //初始页码为0
  36. [containerView addSubview:_pageControl];
  37. //图片张数
  38. _imageNum = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, CGRectGetWidth(containerView.frame)-20, 20)];
  39. _imageNum.font = [UIFont scaleSize:15];
  40. _imageNum.backgroundColor = [UIColor clearColor];
  41. _imageNum.textColor = [UIColor whiteColor];
  42. _imageNum.textAlignment = NSTextAlignmentRight;
  43. // [containerView addSubview:_imageNum];
  44. /*
  45. ***配置定时器,自动滚动广告栏
  46. */
  47. _timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
  48. [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
  49. //[_timer setFireDate:[NSDate distantFuture]];//关闭定时器
  50. }
  51. self.backgroundColor = [UIColor orangeColor];
  52. return self;
  53. }
  54. //---------------------------------------------------------------------------------
  55. -(void)timerAction:(NSTimer *)timer{
  56. if (_totalNum>1) {
  57. CGPoint newOffset = _scrollView.contentOffset;
  58. newOffset.x = newOffset.x + CGRectGetWidth(_scrollView.frame);
  59. // NSLog(@"newOffset.x = %f",newOffset.x);
  60. if (newOffset.x > (CGRectGetWidth(_scrollView.frame) * (_totalNum-1))) {
  61. newOffset.x = 0 ;
  62. }
  63. int index = newOffset.x / CGRectGetWidth(_scrollView.frame); //当前是第几个视图
  64. newOffset.x = index * CGRectGetWidth(_scrollView.frame);
  65. _imageNum.text = [NSString stringWithFormat:@"%d / %d",index+1,(int)_totalNum];
  66. [_scrollView setContentOffset:newOffset animated:YES];
  67. }else{
  68. CGPoint newOffset = _scrollView.contentOffset;
  69. newOffset.x = newOffset.x + CGRectGetWidth(_scrollView.frame);
  70. // NSLog(@"newOffset.x = %f",newOffset.x);
  71. if (newOffset.x > (CGRectGetWidth(_scrollView.frame) * (2))) {
  72. newOffset.x = 0 ;
  73. }
  74. int index = newOffset.x / CGRectGetWidth(_scrollView.frame); //当前是第几个视图
  75. newOffset.x = index * CGRectGetWidth(_scrollView.frame);
  76. _imageNum.text = [NSString stringWithFormat:@"%d / %d",index+1,3];
  77. [_scrollView setContentOffset:newOffset animated:YES];
  78. //[_timer setFireDate:[NSDate distantFuture]];//关闭定时器
  79. }
  80. }
  81. #pragma mark- PageControl绑定ScrollView
  82. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  83. //滚动就执行--手动滑动后,定时器要重新计时dansonmark
  84. if ([scrollView isMemberOfClass:[UITableView class]]) {
  85. }else {
  86. int index = fabs(scrollView.contentOffset.x) / scrollView.frame.size.width; //当前是第几个视图
  87. _pageControl.currentPage = index;
  88. for (UIView *view in scrollView.subviews) {
  89. if(view.tag == index){
  90. }else{
  91. }
  92. }
  93. }
  94. }
  95. - (void)setImgArray:(NSArray *)imgArray{
  96. _imgArray = imgArray;
  97. _totalNum = [imgArray count];
  98. //NSLog(@"广告位图片imgArray---->%@",imgArray);
  99. if (_totalNum > 0) {
  100. for (int i = 0; i<_totalNum; i++) {
  101. NSDictionary *dic = imgArray[i];
  102. UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(i*CGRectGetWidth(_scrollView.frame), 0, CGRectGetWidth(_scrollView.frame), CGRectGetHeight(_scrollView.frame))];
  103. img.contentMode = UIViewContentModeScaleAspectFill;
  104. //img.image = [UIImage imageNamed:imgArray[i]];
  105. [img setClipsToBounds:YES];
  106. NSString *path = dic[@"IMG"];
  107. if (path && ![path hasPrefix:@"http"]){
  108. path = [imgPreFix stringByAppendingString:path];
  109. }
  110. [img sd_setImageWithURL:[NSURL URLWithString:path] placeholderImage:[UIImage imageNamed:@"subject_main_banner.jpg"]];
  111. [img setTag:i];
  112. //把手势添加到指定视图上
  113. [img setUserInteractionEnabled:YES];
  114. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(skipToADLink:)];
  115. [img addGestureRecognizer:tapGesture];
  116. [_scrollView addSubview:img];
  117. }
  118. _imageNum.text = [NSString stringWithFormat:@"%d / %d",(int)_pageControl.currentPage+1,(int)_totalNum];
  119. _pageControl.numberOfPages = _totalNum; //设置页数 //滚动范围 600=300*2,分2页
  120. CGRect frame;
  121. frame = _pageControl.frame;
  122. frame.size.width = 15*_totalNum;
  123. _pageControl.frame = frame;
  124. _scrollView.contentSize = CGSizeMake(CGRectGetWidth(_scrollView.frame)*_totalNum,CGRectGetHeight(_scrollView.frame));//滚动范围 600=300*2,分2页
  125. }else{
  126. // UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_scrollView.frame), CGRectGetHeight(_scrollView.frame))];
  127. // [img setImage:[UIImage imageNamed:@"subject_main_banner.jpg"]];
  128. // img.userInteractionEnabled = YES;
  129. // [_scrollView addSubview:img];
  130. // //_imageNum.text = @"提示:滚动栏无数据。";
  131. //
  132. // _scrollView.contentSize = CGSizeMake(CGRectGetWidth(_scrollView.frame),CGRectGetHeight(_scrollView.frame));
  133. NSArray *adArray = @[@"subject_main_banner.jpg",@"subject_main_banner_2.jpg",@"subject_main_banner_3.jpg"];
  134. for (int i = 0; i<adArray.count; i++) {
  135. UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(i*CGRectGetWidth(_scrollView.frame), 0, CGRectGetWidth(_scrollView.frame), CGRectGetHeight(_scrollView.frame))];
  136. img.contentMode = UIViewContentModeScaleAspectFill;
  137. [img setClipsToBounds:YES];
  138. img.image = [UIImage imageNamed:adArray[i]];
  139. [_scrollView addSubview:img];
  140. }
  141. _imageNum.text = [NSString stringWithFormat:@"%d / %d",(int)_pageControl.currentPage+1,(int)adArray.count];
  142. _pageControl.numberOfPages = adArray.count; //设置页数 //滚动范围 600=300*2,分2页
  143. CGRect frame;
  144. frame = _pageControl.frame;
  145. frame.size.width = 15*adArray.count;
  146. _pageControl.frame = frame;
  147. _scrollView.contentSize = CGSizeMake(CGRectGetWidth(_scrollView.frame)*3,CGRectGetHeight(_scrollView.frame));//滚动范围 600=300*2,分2页
  148. }
  149. }
  150. //程序内跳转
  151. -(void)skipToADLink:(UITapGestureRecognizer *)gesture
  152. {
  153. NSInteger index = gesture.view.tag;
  154. NSDictionary *dic = _imgArray[index];
  155. NSString *skipType = dic[@"OPENTYPE"];
  156. if ([skipType isEqualToString:@"1"]) {
  157. ADLinkVC *ad = [[ADLinkVC alloc] init];
  158. ad.title = dic[@"TITLE"];
  159. ad.urlString = dic[@"URL"];
  160. if (ad.urlString && ad.urlString.length != 0) {
  161. [_supVC navPushHideTabbarToVC:ad];
  162. }
  163. }else{
  164. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:dic[@"URL"]]];
  165. }
  166. }
  167. - (void)openTimer{
  168. [_timer setFireDate:[NSDate distantPast]];//开启定时器
  169. }
  170. - (void)closeTimer{
  171. [_timer setFireDate:[NSDate distantFuture]];//关闭定时器
  172. }
  173. @end