//
|
// subregionView.m
|
// BuWanVideo2.0
|
//
|
// Created by weikou on 16/8/1.
|
// Copyright © 2016年 com.yeshi.buwansheque.ios. All rights reserved.
|
//
|
|
#import "subregionView.h"
|
#import "SubregionViewCollectionViewCell.h"
|
|
#import "SubregionDetailViewController.h"
|
//#import "GDTNativeAd.h"//广点通原生广告
|
#import "ADCollectionReusableView.h"
|
#import "GDTNativeExpressAd.h"
|
#import "GDTNativeExpressAdView.h"
|
#import "FaxianCReusableView.h"
|
//#import "BannerViewController.h"
|
#import "GDTUnifiedBannerView.h"
|
|
@interface subregionView ()<UICollectionViewDelegate,UICollectionViewDataSource,GDTNativeExpressAdDelegete,GDTUnifiedBannerViewDelegate>{
|
UICollectionView *_collview;
|
// GDTNativeAd *_nativeAd; //原生广告实例
|
// NSArray *nativeArray;//存储请求下来的原生广告信息
|
|
}
|
@property (nonatomic , strong) NSMutableArray *dataClass;//分区数据
|
@property (nonatomic, strong) NSArray *expressAdViews;
|
@property (nonatomic, strong) GDTNativeExpressAd *nativeExpressAd;
|
@property (nonatomic, strong) GDTUnifiedBannerView *bannerView;
|
|
@end
|
@implementation subregionView
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
{
|
self = [super initWithFrame:frame];
|
if (self) {
|
//从file中读取数据
|
[self loadAd];
|
[self loadDataFromFile];
|
//配置collectionView的属性
|
UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
|
flowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;//滚动方向
|
flowlayout.minimumLineSpacing = (KScreenW)/10;//行间距
|
flowlayout.minimumInteritemSpacing=(KScreenW)/10;//item之间的距离
|
flowlayout.itemSize = CGSizeMake((KScreenW)/6, (KScreenW)/6+30);//设置itme的大小
|
flowlayout.sectionInset = UIEdgeInsetsMake((KScreenW)/10,(KScreenW)/9,(KScreenW)/10,(KScreenW)/9);//设置itme之间的间距
|
flowlayout.headerReferenceSize = CGSizeMake(0, 60);
|
//创建collectionView视图
|
if (!_collview) {
|
_collview = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:flowlayout];
|
}
|
_collview.scrollEnabled = YES;
|
_collview.backgroundColor = [UIColor whiteColor];
|
_collview.delegate = self;
|
_collview.dataSource = self;
|
[self addSubview:_collview];
|
|
//给collview视图注册cell
|
[_collview registerNib:[UINib nibWithNibName:@"SubregionViewCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"SubregionViewCollectionViewCell"];
|
[_collview registerNib:[UINib nibWithNibName:@"ADCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ADCollectionReusableView"];
|
|
[_collview registerNib:[UINib nibWithNibName:@"FaxianCReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FaxianCReusableView"];
|
|
//给collectionView设置下拉刷新
|
_collview.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
|
//开始刷新
|
[_collview.mj_header beginRefreshing];
|
//网络请求
|
[self loadNewData];
|
|
}];
|
|
}
|
return self;
|
}
|
|
/**
|
* 下拉刷新调用的方法
|
*/
|
- (void)loadNewData{
|
[self getchannelData];
|
}
|
|
/**
|
* 请求分区数据
|
*/
|
- (void)getchannelData{
|
[[YTHNetInterface startInterface] getClassWithUid:[YTHsharedManger startManger].Uid withSystem:@"1" withBlock:^(BOOL isSuccessful, id result, NSString *error) {
|
if (isSuccessful) {
|
NSDictionary *dic = (NSDictionary *)result;
|
if (!_dataClass) {
|
_dataClass = [[NSMutableArray alloc ] initWithCapacity:0];
|
}
|
[_dataClass removeAllObjects];
|
NSArray *ar = [[dic objectForKey:@"Data"] objectForKey:@"data"];
|
for (int i = 0; i<ar.count; i++) {
|
if ([[ar objectAtIndex:i][@"Id"]integerValue] == 309 || [[ar objectAtIndex:i][@"Id"]integerValue] == 310) {
|
continue;
|
}
|
NSLog(@"---- %@ ----",[ar objectAtIndex:i]);
|
[_dataClass addObject:[ar objectAtIndex:i]];
|
}
|
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:_dataClass];
|
[data writeToFile:CHANNELfILE atomically:YES];
|
//刷新collectionView视图
|
[_collview reloadData];
|
[self loadAd];
|
}else{
|
|
}
|
//结束网络刷新
|
[_collview.mj_header endRefreshing];
|
}];
|
}
|
|
/**
|
* 从沙盒里面加载数据
|
*/
|
-(void)loadDataFromFile{
|
NSFileManager *fileManager = [NSFileManager defaultManager];
|
if ([fileManager fileExistsAtPath:CHANNELfILE])
|
{
|
_dataClass = [NSKeyedUnarchiver unarchiveObjectWithData:[NSData dataWithContentsOfFile:CHANNELfILE]];
|
}else{
|
[self loadNewData];
|
}
|
}
|
|
/**
|
* 返回当前视图的控制器
|
*/
|
- (UIViewController *)viewController {
|
for (UIView* next = [self superview]; next; next = next.superview) {
|
UIResponder *nextResponder = [next nextResponder];
|
if ([nextResponder isKindOfClass:[UIViewController class]]) {
|
return (UIViewController *)nextResponder;
|
}
|
}
|
return nil;
|
}
|
/**
|
* 点击广点通广告的方法
|
*/
|
//-(void)getMoreInformation:(UIButton *)sender{
|
// GDTNativeAdData * info;
|
// if (sender.tag-456==0) {
|
// info=nativeArray[0];
|
// }
|
//
|
// [_nativeAd clickAd:info];
|
//
|
//}
|
//-(void)nativeAdSuccessToLoad:(NSArray *)nativeAdDataArray{
|
//
|
// nativeArray = nativeAdDataArray;
|
// [_collview reloadData];;
|
//}
|
//- (void)nativeAdFailToLoad:(NSError *)error{
|
// NSLog(@"%@",error);
|
//}
|
/**
|
* 加载广点通原生广告
|
*/
|
-(void)loadAd{
|
// _nativeAd = [[GDTNativeAd alloc]initWithAppId: GDTADkey placementId:GDTYSADkey5];
|
// _nativeAd.controller = [self topViewController];
|
// _nativeAd.delegate = self;
|
// [_nativeAd loadAd:1];
|
|
// 支持视频广告的 PlacementId 会混出视频与图片广告
|
|
self.nativeExpressAd =[[GDTNativeExpressAd alloc] initWithPlacementId:@"6000418684535350" adSize:CGSizeMake(KScreenW, (KScreenW-20)/16*9 + 10)];
|
self.nativeExpressAd.delegate = self;
|
// 配置视频播放属性
|
self.nativeExpressAd.videoAutoPlayOnWWAN = YES;
|
self.nativeExpressAd.videoMuted = NO;
|
[self.nativeExpressAd loadAd:1];
|
|
}
|
- (void)clickRemoveAd:(id)sender {
|
[self.bannerView removeFromSuperview];
|
self.bannerView = nil;
|
}
|
- (void)clickLoadAd:(id)sender {
|
|
}
|
// 请求广告条数据成功后调用
|
- (void)unifiedBannerViewDidLoad:(GDTUnifiedBannerView *)unifiedBannerView {
|
|
}
|
// 请求广告条数据失败后调用
|
- (void)unifiedBannerViewFailedToLoad:(GDTUnifiedBannerView *)unifiedBannerView error:(NSError *)error {
|
|
}
|
|
- (GDTUnifiedBannerView *)bannerView {
|
if (_bannerView) {
|
return _bannerView;
|
}
|
CGRect rect = CGRectMake(0, 0, KScreenW, 60);
|
//_bannerView = [[GDTUnifiedBannerView alloc] initWithFrame:rect appId:GDTADkey placementId:GDTYSADkey5];
|
_bannerView = [[GDTUnifiedBannerView alloc] initWithFrame:rect placementId:GDTYSADkey5 viewController:[self topViewController]];
|
//设置广告轮播时间,范围为30-120秒,0表示不轮播
|
_bannerView.autoSwitchInterval = 0;
|
//设置Delegate
|
_bannerView.delegate = self;
|
return _bannerView;
|
}
|
|
- (void)nativeExpressAdSuccessToLoad:(GDTNativeExpressAd
|
*)nativeExpressAd views:(NSArray<__kindof
|
GDTNativeExpressAdView *> *)views
|
{
|
self.expressAdViews = [NSArray arrayWithArray:views];
|
if (self.expressAdViews.count) {
|
[self.expressAdViews enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
|
GDTNativeExpressAdView *expressView = (GDTNativeExpressAdView *)obj;
|
expressView.controller = [self topViewController];;
|
[expressView render];
|
}];
|
}
|
// 广告位 render 后刷新
|
[_collview reloadData];
|
}
|
- (void)nativeExpressAdFailToLoad:(GDTNativeExpressAd *)nativeExpressAd error:(NSError *)error{
|
NSLog(@"%@",error);
|
}
|
|
- (UIViewController *)_topViewController:(UIViewController *)vc {
|
if ([vc isKindOfClass:[UINavigationController class]]) {
|
return [self _topViewController:[(UINavigationController *)vc topViewController]];
|
} else if ([vc isKindOfClass:[UITabBarController class]]) {
|
return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
|
} else {
|
return vc;
|
}
|
return nil;
|
}
|
- (UIViewController *)topViewController {
|
|
UIViewController *resultVC;
|
resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
|
while (resultVC.presentedViewController) {
|
resultVC = [self _topViewController:resultVC.presentedViewController];
|
}
|
return resultVC;
|
}
|
#pragma mark -UICollectionViewDelegate
|
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
|
SubregionDetailViewController *subregionVC=[[SubregionDetailViewController alloc] init];
|
subregionVC.Id=[[_dataClass objectAtIndex:indexPath.row] objectForKey:@"Id"];
|
subregionVC.titles=[[_dataClass objectAtIndex:indexPath.row] objectForKey:@"Name"];
|
[subregionVC setHidesBottomBarWhenPushed:YES];
|
|
[[self viewController].navigationController pushViewController:subregionVC animated:YES];
|
}
|
|
#pragma mark -UICollectionViewDataSource
|
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
|
return _dataClass.count;
|
}
|
|
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
|
SubregionViewCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"SubregionViewCollectionViewCell" forIndexPath:indexPath];
|
[cell.image setContentMode:UIViewContentModeScaleAspectFill];
|
[cell.image setYthImageWithURL:[[_dataClass objectAtIndex:indexPath.row] objectForKey:@"Icon"] placeholderImage:[UIImage imageNamed:@"默认加载图片"]];
|
cell.text.text=[[_dataClass objectAtIndex:indexPath.row] objectForKey:@"Name"];
|
return cell;
|
}
|
|
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
|
if (kind == UICollectionElementKindSectionHeader){
|
// ADCollectionReusableView *groupfootSection = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ADCollectionReusableView" forIndexPath:indexPath];
|
//
|
// if (indexPath.section == 0) {
|
// //返回广告视图
|
// GDTNativeAdData *info;
|
// info=nativeArray[0];
|
// groupfootSection.ADTitle.text=[info.properties objectForKey:GDTNativeAdDataKeyTitle];
|
// groupfootSection.ADTitle.backgroundColor=[UIColor clearColor];
|
// [groupfootSection.ADImage setYthImageWithURL:[info.properties objectForKey:GDTNativeAdDataKeyImgUrl] placeholderImage:nil];
|
// //把button设为透明
|
// groupfootSection.ADClickButton.backgroundColor=[UIColor clearColor];
|
// [groupfootSection.ADClickButton setTag:456+indexPath.section];
|
// [groupfootSection.ADClickButton addTarget:self action:@selector(getMoreInformation:) forControlEvents:UIControlEventTouchUpInside];
|
// [_nativeAd attachAd:info toView:groupfootSection];
|
// }
|
// return groupfootSection;
|
// GDTNativeExpressAdView *adView ;
|
// if (self.expressAdViews.count > 0) {
|
// adView = (GDTNativeExpressAdView *)self.expressAdViews[0];
|
// }
|
FaxianCReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FaxianCReusableView" forIndexPath:indexPath];
|
if (indexPath.section == 0) {
|
[self clickRemoveAd:nil];
|
|
[view addSubview:self.bannerView];
|
[self.bannerView loadAdAndShow];
|
|
}
|
return view;
|
|
}else{
|
FaxianCReusableView *groupfootSection = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FaxianCReusableView" forIndexPath:indexPath];
|
// UIView *view frame=CGRectMake(0, 0, KScreenW, KScreenW/2);
|
// [groupfootSection addSubview:self.nativeAdView];
|
return groupfootSection;
|
}
|
|
}
|
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
|
// if (section==0) {
|
// return CGSizeMake(KScreenW, (KScreenW-20)/16*9);
|
//
|
// }
|
// return CGSizeMake(0, 0);
|
//}
|
@end
|