AdvertisingColumn.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 "RQCustomWebViewViewController.h"
  10. //#import "WXApiRequestHandler.h"
  11. //#import "WXApiManager.h"
  12. //#import "WechatAuthSDK.h"
  13. @interface AdvertisingColumn () //<WXApiManagerDelegate, WechatAuthAPIDelegate>
  14. @end
  15. @implementation AdvertisingColumn
  16. - (id)initWithFrame:(CGRect)frame
  17. {
  18. self = [super initWithFrame:frame];
  19. if (self) {
  20. _scrollView = [[UIScrollView alloc]initWithFrame:frame];
  21. _scrollView.delegate = self;//设置代理UIscrollViewDelegate
  22. _scrollView.showsVerticalScrollIndicator = NO;//是否显示竖向滚动条
  23. _scrollView.showsHorizontalScrollIndicator = NO;//是否显示横向滚动条
  24. _scrollView.pagingEnabled = YES;//是否设置分页
  25. _scrollView.bounces = NO;
  26. [self addSubview:_scrollView];
  27. /*
  28. ***容器,装载
  29. */
  30. UIView *containerView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.frame)-20, CGRectGetWidth(self.frame), 20)];
  31. containerView.backgroundColor = [UIColor clearColor];
  32. [self addSubview:containerView];
  33. UIView *alphaView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(containerView.frame), CGRectGetHeight(containerView.frame))];
  34. alphaView.alpha = 0.7;
  35. [containerView addSubview:alphaView];
  36. //分页控制
  37. _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(kSize.width*0.5 - 15, 0, kSize.width, 20)];
  38. _pageControl.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  39. _pageControl.currentPage = 0; //初始页码为0
  40. [containerView addSubview:_pageControl];
  41. //图片张数
  42. _imageNum = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, CGRectGetWidth(containerView.frame)-20, 20)];
  43. _imageNum.font = [UIFont scaleSize:15];
  44. _imageNum.backgroundColor = [UIColor clearColor];
  45. _imageNum.textColor = [UIColor whiteColor];
  46. _imageNum.textAlignment = NSTextAlignmentRight;
  47. // [containerView addSubview:_imageNum];
  48. /*
  49. ***配置定时器,自动滚动广告栏
  50. */
  51. _timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
  52. [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
  53. //[_timer setFireDate:[NSDate distantFuture]];//关闭定时器
  54. }
  55. self.backgroundColor = [UIColor orangeColor];
  56. // [WXApiManager sharedManager].delegate = self;
  57. return self;
  58. }
  59. //---------------------------------------------------------------------------------
  60. -(void)timerAction:(NSTimer *)timer{
  61. if (_totalNum>1) {
  62. CGPoint newOffset = _scrollView.contentOffset;
  63. newOffset.x = newOffset.x + CGRectGetWidth(_scrollView.frame);
  64. // NSLog(@"newOffset.x = %f",newOffset.x);
  65. if (newOffset.x > (CGRectGetWidth(_scrollView.frame) * (_totalNum-1))) {
  66. newOffset.x = 0 ;
  67. }
  68. int index = newOffset.x / CGRectGetWidth(_scrollView.frame); //当前是第几个视图
  69. newOffset.x = index * CGRectGetWidth(_scrollView.frame);
  70. _imageNum.text = [NSString stringWithFormat:@"%d / %d",index+1,(int)_totalNum];
  71. [_scrollView setContentOffset:newOffset animated:YES];
  72. }else{
  73. [_timer setFireDate:[NSDate distantFuture]];//关闭定时器
  74. }
  75. }
  76. #pragma mark- PageControl绑定ScrollView
  77. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  78. //滚动就执行--手动滑动后,定时器要重新计时dansonmark
  79. if ([scrollView isMemberOfClass:[UITableView class]]) {
  80. }else {
  81. int index = fabs(scrollView.contentOffset.x) / scrollView.frame.size.width; //当前是第几个视图
  82. _pageControl.currentPage = index;
  83. for (UIView *view in scrollView.subviews) {
  84. if(view.tag == index){
  85. }else{
  86. }
  87. }
  88. }
  89. }
  90. - (void)setImgArray:(NSArray *)imgArray{
  91. _imgArray = imgArray;
  92. _totalNum = [imgArray count];
  93. //NSLog(@"广告位图片imgArray---->%@",imgArray);
  94. if (_totalNum > 0) {
  95. for (int i = 0; i<_totalNum; i++) {
  96. NSDictionary *dic = imgArray[i];
  97. UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(i*CGRectGetWidth(_scrollView.frame), 0, CGRectGetWidth(_scrollView.frame), CGRectGetHeight(_scrollView.frame))];
  98. img.contentMode = UIViewContentModeScaleAspectFill;
  99. //img.image = [UIImage imageNamed:imgArray[i]];
  100. [img setClipsToBounds:YES];
  101. NSString *path = dic[@"IMG"];
  102. if (path && ![path hasPrefix:@"http"] && ![path hasPrefix:@"https"]){
  103. path = [imgPreFix stringByAppendingString:path];
  104. }
  105. // [img sd_setImageWithURL:[NSURL URLWithString:path] placeholderImage:[UIImage imageNamed:@"subject_main_banner.png"]];
  106. [img sd_setImageWithURL:[NSURL URLWithString:path] placeholderImage:[UIImage imageNamed:@"subject_main_banner.png"] options:SDWebImageAllowInvalidSSLCertificates];
  107. [img setTag:i];
  108. //把手势添加到指定视图上
  109. [img setUserInteractionEnabled:YES];
  110. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(skipToADLink:)];
  111. [img addGestureRecognizer:tapGesture];
  112. [_scrollView addSubview:img];
  113. }
  114. _imageNum.text = [NSString stringWithFormat:@"%d / %d",(int)_pageControl.currentPage+1,(int)_totalNum];
  115. _pageControl.numberOfPages = _totalNum; //设置页数 //滚动范围 600=300*2,分2页
  116. CGRect frame;
  117. frame = _pageControl.frame;
  118. frame.size.width = 15*_totalNum;
  119. _pageControl.frame = frame;
  120. _scrollView.contentSize = CGSizeMake(CGRectGetWidth(_scrollView.frame)*_totalNum,CGRectGetHeight(_scrollView.frame));//滚动范围 600=300*2,分2页
  121. }else{
  122. UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_scrollView.frame), CGRectGetHeight(_scrollView.frame))];
  123. [img setImage:[UIImage imageNamed:@"subject_main_banner.png"]];
  124. img.userInteractionEnabled = YES;
  125. // [img setTapActionWithBlock:^(UITapGestureRecognizer *tap) {
  126. // [self miniwithUserName:@"gh_a994fd2f498e"];
  127. // }];
  128. [_scrollView addSubview:img];
  129. //_imageNum.text = @"提示:滚动栏无数据。";
  130. _scrollView.contentSize = CGSizeMake(CGRectGetWidth(_scrollView.frame),CGRectGetHeight(_scrollView.frame));
  131. }
  132. }
  133. //程序内跳转
  134. - (void)skipToADLink:(UITapGestureRecognizer *)gesture {
  135. NSInteger index = gesture.view.tag;
  136. NSDictionary *dic = _imgArray[index];
  137. [RQ_SHARE_FUNCTION saveObjectWithObject:dic ForKey:@"currentSelectDic"];
  138. NSString *skipType = dic[@"OPENTYPE"];
  139. // skipType = @"1";
  140. // NSString *url = @"https://mobile.yangkeduo.com/duo_transfer_channel.html?resourceType=39997&pid=1642187_203462988&cpsSign=CE_210421_1642187_203462988_d9346faa8446cc110bb7c100859c9691&duoduo_type=2";
  141. if ([skipType isEqualToString:@"1"]) {
  142. [RQ_SHARE_FUNCTION gotoWebViewWithUrlStr:dic[@"URL"]];
  143. }else{
  144. if ([dic[@"ADTYPE"] isEqualToString:@"2"]) {
  145. [RQ_SHARE_FUNCTION miniwithUserName:dic[@"URL"] path:dic[@"AI_XCX_URL"]? : @""];
  146. } else if ([dic[@"ADTYPE"] isEqualToString:@"1"]) {
  147. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:dic[@"URL"]] options:nil completionHandler:^(BOOL success) {
  148. }];
  149. } else if ([dic[@"ADTYPE"] isEqualToString:@"3"]) {
  150. WXSubscribeMsgReq *req = [[WXSubscribeMsgReq alloc] init];
  151. req.scene = 1;
  152. req.templateId = @"1iZwYeI2QtSXZ3rTRkQ42wgRkd7NSINzem8R-LvtHYw";
  153. req.reserved = @"";
  154. [WXApi sendReq:req completion:nil];
  155. }
  156. }
  157. }
  158. //- (void)managerDidRecvSubscribeMsgResponse:(WXSubscribeMsgResp *)response {
  159. // NSDictionary *currentSelectDic = [RQ_SHARE_FUNCTION getObjectWithKey:@"currentSelectDic"];
  160. // [jiaPeiManager requestGetAnythingWithURL:[NSString stringWithFormat:@"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%@&secret=%@",RQ_WECHAT_MANAGER.appID, RQ_WECHAT_MANAGER.appSecret] completion:^(NSDictionary *dic) {
  161. // if (dic[@"access_token"]) {
  162. // [jiaPeiManager requestAnythingWithURL:[NSString stringWithFormat:@"https://api.weixin.qq.com/cgi-bin/message/template/subscribe?access_token=%@",dic[@"access_token"]] array:@[
  163. // @{@"touser" : response.openId},
  164. // @{@"template_id" : response.templateId},
  165. // @{@"url" : currentSelectDic[@"URL"]? : @"https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=Mzk0MTI1ODAyMA==#wechat_redirect"},
  166. // @{@"scene" : [NSNumber numberWithInt:response.scene]},
  167. // @{@"title" : currentSelectDic[@"TITLE"]? : @"消息"},
  168. // @{@"data" : @{@"content":@{@"color":@"#173177",@"value":currentSelectDic[@"AI_CONTENT"]? : @"点击关注公众号"}}}
  169. // ].mutableCopy data:nil completion:^(NSDictionary *dict) {
  170. // if ([dict[@"errcode"] integerValue] == 0) {
  171. // dispatch_async(dispatch_get_main_queue(), ^{
  172. // ShowMsg(@"成功");
  173. // });
  174. // } else {
  175. // ShowMsg(@"失败");
  176. // }
  177. // }];
  178. // } else {
  179. // ShowMsg(@"获取token失败");
  180. // }
  181. // }];
  182. //
  183. //}
  184. - (void)openTimer{
  185. [_timer setFireDate:[NSDate distantPast]];//开启定时器
  186. }
  187. - (void)closeTimer{
  188. [_timer setFireDate:[NSDate distantFuture]];//关闭定时器
  189. }
  190. @end