1.创建CreatModel继承NSObject
在CreatModel.h里定义属性:
//主标题
@property(nonatomic,copy)NSString *fileDescribe;
//主图片
@property(nonatomic,copy)NSString *imageUrl;
//视频内容
@property(nonatomic,copy)NSString *fileUrl;
在CreatModel.m里:
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
其次创建CollectionViewCell 继承于 UICollectionViewCell
在.h里面:
@interface CollectionViewCell : UICollectionViewCell
@property(nonatomic,strong)UIImageView *img;
@property(nonatomic,strong)UILabel *titleLabel;
@end
在.m里:
-(instancetype)initWithFrame:(CGRect)frame{
self=[super initWithFrame:frame];
if (self) {
self.backgroundColor=[UIColor lightGrayColor];
self.img=[[UIImageView alloc]initWithFrame:CGRectMake(20, 0, 180, 180)];
[self addSubview:self.img];
self.titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 170, 160, 60)];
self.titleLabel.numberOfLines=0;
self.titleLabel.textColor=[UIColor whiteColor];
[self addSubview:self.titleLabel];
}
return self;
}
@end
其次是VC.m里面:
#import “XiaoShipinViewController.h”
#import “CollectionViewCell.h”
#import <AFNetworking.h>
#import “VideoController.h”
#import <SVProgressHUD.h>
#import “CreatModel.h”
#define fDeviceWidth [UIScreen mainScreen].bounds.size.width
#define fDeviceHeight [UIScreen mainScreen].bounds.size.height
#define url @“http://123.126.40.109:7003/asmr/videos/A1100101.shtml”
@interface XiaoShipinViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>{
UIView *View;
UIImageView *img;
UIButton *butt;
BOOL bol;
}
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *dataSource;
@property(nonatomic,strong)NSMutableArray *dataSourceLabel;
@property(nonatomic,strong)NSMutableArray *dataSourceImg;
@end
// 声明三个重用修饰符.
static NSString *reuseID = @“cell”;
static NSString *footerReuseID = @“footerView”;
static NSString *headerReuseID = @“headerView”;
@implementation XiaoShipinViewController
-
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.self.dataSource=[[NSMutableArray alloc]init];
self.dataSourceImg=[[NSMutableArray alloc]init];
self.dataSourceLabel=[[NSMutableArray alloc]init];
UILabel *lab=[[UILabel alloc]init];
lab.text=@“视频”;
lab.textColor=[UIColor whiteColor];
self.navigationController.navigationBar.barTintColor=[UIColor purpleColor];
lab.font=[UIFont systemFontOfSize:20];
self.navigationItem.titleView=lab;
self.view.backgroundColor=[UIColor redColor];
[self setupCollectionView];
[self loadData];
}
-(void)loadData{
[SVProgressHUD showInfoWithStatus:@“正在加载中…”];
//创建网络请求
AFHTTPSessionManager *manger=[AFHTTPSessionManager manager];
//设置默认请求类型(NSData)
manger.responseSerializer=[AFHTTPResponseSerializer serializer];
[manger GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//解析数据
NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:1 error:nil];
NSArray *resultArray=dict[@"result"];
for (NSDictionary *dic in resultArray) {
CreatModel *model=[CreatModel new];
[model setValuesForKeysWithDictionary:dic];
[self.dataSourceImg addObject:dic[@"imageUrl"]];
[self.dataSourceLabel addObject:dic[@"fileDescribe"]];
[self.dataSource addObject:model];
}
[SVProgressHUD dismiss];
//刷新界面
[self performSelectorOnMainThread:@selector(reloadTable) withObject:nil waitUntilDone:NO];
NSLog(@"请求数据成功");
NSLog(@"%@",self.dataSource);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[SVProgressHUD dismiss];
NSLog(@"请求数据失败");
}];
}
//刷新表格
-(void)reloadTable{
[self.collectionView reloadData];
}
-(void)setupCollectionView{
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
flowLayout.itemSize= CGSizeMake(180, 180);
// 设置滚动方向
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
// 设置最小列间距
flowLayout.minimumInteritemSpacing = 30;
// 设置最小行间距
flowLayout.minimumLineSpacing = 35;
// 设置分区间距
// flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 35, 25);
// 创建网格对象(网格视图.)
self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowLayout];
// 设置代理
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.backgroundColor = [UIColor whiteColor];
// 将网格添加到视图上
[self.view addSubview:self.collectionView];
// 注册 cell
[_collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:reuseID];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.dataSource.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];
cell.img.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.dataSourceImg[indexPath.row]]]];
cell.titleLabel.text=self.dataSourceLabel[indexPath.row];
cell.titleLabel.textColor=[UIColor redColor];
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
[collectionView deselectItemAtIndexPath:indexPath animated:NO];
VideoController *detail=[VideoController new];
CreatModel *model=self.dataSource[indexPath.row];
detail.fileUrl=model.fileUrl;
detail.hidesBottomBarWhenPushed=YES;
[self.navigationController pushViewController:detail animated:YES];
}
@end
最后在另一个控制器VideoController.h里面:
引入框架
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
NS_ASSUME_NONNULL_BEGIN
@interface VideoController : MPMoviePlayerViewController
@property(nonatomic,strong)NSString *fileUrl;
@end
随后在.m中:
#import “VideoController.h”
@implementation VideoController
-(void)viewDidLoad{
self.moviePlayer.contentURL=[NSURL URLWithString:self.fileUrl];
[self.moviePlayer play];
[self createBackBtn];
}
-(void)createBackBtn{
UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(pushBack) forControlEvents:UIControlEventTouchUpInside];
}
-(void)pushBack{
[self.navigationController popViewControllerAnimated:YES];
self.tabBarController.tabBar.hidden=NO;
[self.moviePlayer pause];
}
-(void)viewWillAppear:(BOOL)animated{
self.navigationController.navigationBar.hidden=YES;
self.tabBarController.tabBar.hidden=YES;
}
-(void)viewWillDisappear:(BOOL)animated{
self.navigationController.navigationBar.hidden=NO;
}
@end