// // AdvertisingColumn.m // CustomTabBar // // Created by shikee_app05 on 14-12-30. // Copyright (c) 2014年 chan kaching. All rights reserved. // #import "AdvertisingColumn.h" #import "RQCustomWebViewViewController.h" //#import "WXApiRequestHandler.h" //#import "WXApiManager.h" //#import "WechatAuthSDK.h" @interface AdvertisingColumn () // @end @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]; // [WXApiManager sharedManager].delegate = self; 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{ //滚动就执行--手动滑动后,定时器要重新计时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 hasPrefix:@"https"]){ path = [imgPreFix stringByAppendingString:path]; } // [img sd_setImageWithURL:[NSURL URLWithString:path] placeholderImage:[UIImage imageNamed:@"subject_main_banner.png"]]; [img sd_setImageWithURL:[NSURL URLWithString:path] placeholderImage:[UIImage imageNamed:@"subject_main_banner.png"] options:SDWebImageAllowInvalidSSLCertificates]; [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.png"]]; img.userInteractionEnabled = YES; // [img setTapActionWithBlock:^(UITapGestureRecognizer *tap) { // [self miniwithUserName:@"gh_a994fd2f498e"]; // }]; [_scrollView addSubview:img]; //_imageNum.text = @"提示:滚动栏无数据。"; _scrollView.contentSize = CGSizeMake(CGRectGetWidth(_scrollView.frame),CGRectGetHeight(_scrollView.frame)); } } //程序内跳转 - (void)skipToADLink:(UITapGestureRecognizer *)gesture { NSInteger index = gesture.view.tag; NSDictionary *dic = _imgArray[index]; [RQ_SHARE_FUNCTION saveObjectWithObject:dic ForKey:@"currentSelectDic"]; NSString *skipType = dic[@"OPENTYPE"]; // skipType = @"1"; // NSString *url = @"https://mobile.yangkeduo.com/duo_transfer_channel.html?resourceType=39997&pid=1642187_203462988&cpsSign=CE_210421_1642187_203462988_d9346faa8446cc110bb7c100859c9691&duoduo_type=2"; if ([skipType isEqualToString:@"1"]) { [RQ_SHARE_FUNCTION gotoWebViewWithUrlStr:dic[@"URL"]]; }else{ if ([dic[@"ADTYPE"] isEqualToString:@"2"]) { [RQ_SHARE_FUNCTION miniwithUserName:dic[@"URL"] path:dic[@"AI_XCX_URL"]? : @""]; } else if ([dic[@"ADTYPE"] isEqualToString:@"1"]) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:dic[@"URL"]] options:nil completionHandler:^(BOOL success) { }]; } else if ([dic[@"ADTYPE"] isEqualToString:@"3"]) { WXSubscribeMsgReq *req = [[WXSubscribeMsgReq alloc] init]; req.scene = 1; req.templateId = @"1iZwYeI2QtSXZ3rTRkQ42wgRkd7NSINzem8R-LvtHYw"; req.reserved = @""; [WXApi sendReq:req completion:nil]; } } } //- (void)managerDidRecvSubscribeMsgResponse:(WXSubscribeMsgResp *)response { // NSDictionary *currentSelectDic = [RQ_SHARE_FUNCTION getObjectWithKey:@"currentSelectDic"]; // [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) { // if (dic[@"access_token"]) { // [jiaPeiManager requestAnythingWithURL:[NSString stringWithFormat:@"https://api.weixin.qq.com/cgi-bin/message/template/subscribe?access_token=%@",dic[@"access_token"]] array:@[ // @{@"touser" : response.openId}, // @{@"template_id" : response.templateId}, // @{@"url" : currentSelectDic[@"URL"]? : @"https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=Mzk0MTI1ODAyMA==#wechat_redirect"}, // @{@"scene" : [NSNumber numberWithInt:response.scene]}, // @{@"title" : currentSelectDic[@"TITLE"]? : @"消息"}, // @{@"data" : @{@"content":@{@"color":@"#173177",@"value":currentSelectDic[@"AI_CONTENT"]? : @"点击关注公众号"}}} // ].mutableCopy data:nil completion:^(NSDictionary *dict) { // if ([dict[@"errcode"] integerValue] == 0) { // dispatch_async(dispatch_get_main_queue(), ^{ // ShowMsg(@"成功"); // }); // } else { // ShowMsg(@"失败"); // } // }]; // } else { // ShowMsg(@"获取token失败"); // } // }]; // //} - (void)openTimer{ [_timer setFireDate:[NSDate distantPast]];//开启定时器 } - (void)closeTimer{ [_timer setFireDate:[NSDate distantFuture]];//关闭定时器 } @end