PCH
绝对路径: Build Settings->Prefix Header:$(SRCROOT)/文件路径/PrefixHeader.pch/
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT ([UIScreen mainScreen].bounds.size.height - 64)
#define RGB(r,g,b,a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]
//头文件
#import “MJRefresh.h”//刷新
#import “AFNetworking.h”//网络请求
#import “UIKit+AFNetworking.h”
#import “SVProgressHUD.h”//加载栏
#import <UIImageView+WebCache.h>/图片异步加载/
AppDelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (readonly, strong) NSPersistentContainer *persistentContainer;
- (void)saveContext;
AppDelegate.m
self.window.rootViewController = [[MyTabbarController alloc]init];
self.window.backgroundColor=[UIColor whiteColor];
MyTabbarController.h
@interface MyTabbarController : UITabBarController
MyTabbarController.m
#import “MyTabbarController.h”
@interface MyTabbarController ()
@end
@implementation MyTabbarController
-
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSArray *VCNames=@[@“NewsViewController”,@“VdViewController”,@“PictureViewController”,@“JokeViewController”];//视图
NSArray *unSelectImgNames=@[@“新闻”,@“视频”,@“图片”,@“段子”];//图片
NSArray *selectImgNames=@[@“新闻1”,@“视频1”,@“图片1”,@“段子1”];//点击后的图片
NSMutableArray *viewControllers=[NSMutableArray new];
NSArray *arr=@[@“视频”,@“图片”,@“我的”];
for (int i=0; i<VCNames.count; i++) {
UIViewController *vc=[[NSClassFromString(VCNames[i] )alloc] init];
//导航条透明
vc.title=arr[i];//导航条文字UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:vc]; nav.tabBarItem.image=[[UIImage imageNamed:unSelectImgNames[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; nav.tabBarItem.selectedImage=[[UIImage imageNamed:selectImgNames[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; nav.navigationBar.translucent=NO; nav.navigationBar.barTintColor=[UIColor blackColor]; [nav.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]}]; [viewControllers addObject:nav];
}
self.viewControllers=viewControllers;
//底部标签字体颜色
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:RGB(0, 140, 213, 1)} forState:UIControlStateSelected];
}
MVC
Model:建立接口 继承 NSObject
@interface Model : NSObject
//主标题
@property(nonatomic,copy)NSString *fileDescribe;
//主图片
@property(nonatomic,copy)NSString *imageUrl;
//视频
@property(nonatomic,copy)NSString *fileUrl;
@end
Model.m
#import "Model.h"
@implementation Model
-(void)setValue:(id)value forUndefinedKey:(NSString *)key
{
}
@end
View 自定义的网格或表格 继承:UICollectionViewCell/UITableViewCell
VOUICollectionViewCell.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface VOCollectionViewCell : UICollectionViewCell
@property(nonatomic,strong)UIImageView *imagev; //自定义的图片
@property(nonatomic,strong)UILabel *labelI;
@end
NS_ASSUME_NONNULL_END
VOUICollectionViewCell.m
#import "VOCollectionViewCell.h"
#import "VdViewController.h"
@implementation VOCollectionViewCell
//自定义cell
-(instancetype)initWithFrame:(CGRect)frame
{
self=[super initWithFrame:frame];
if (self) {
[self addSubview:self.imagev];
[self addSubview:self.labelI];
}
return self;
}
-(UIImageView *)imagev
{
if (!_imagev) {
_imagev= [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH/2, 150)];
_imagev.layer.masksToBounds = YES;
_imagev.layer.cornerRadius = 50;
}
return _imagev;
}
-(UILabel *)labelI
{
// 判断 如果没有内容 就创建
if (!_labelI) {
// 设置 frame
_labelI = [[UILabel alloc] initWithFrame:CGRectMake(-25, 160, 280, 30)];
// 设置 字体
_labelI.font = [UIFont systemFontOfSize:15];
_labelI.textColor = [UIColor grayColor];
//居中
_labelI.textAlignment = NSTextAlignmentCenter;
}
return _labelI;
}
@end
Controll 继承: UIViewController
VdViewController.m
#import "VdViewController.h"
#import "VOCollectionViewCell.h"
#import "Model.h"
#import "videodetailViewController.h"
@interface VdViewController ()<UICollectionViewDelegate , UICollectionViewDataSource>
{
UICollectionView *clV;
UIView *view;
UIImageView *imgI;
UIButton *btn;
}
@property(nonatomic,assign)int i;
@property(nonatomic,strong)NSMutableArray *dataSource;
@property(nonatomic,strong)NSMutableArray *datalabel;
@property(nonatomic,strong)NSMutableArray *dataimage;
@property(nonatomic,strong)NSMutableArray *datavoice;
@property(nonatomic,copy)NSString *fileDescribe;
@end
@implementation VdViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.i=1;//让打开视图为1
//初始化数据源
self.dataSource=[[NSMutableArray alloc]init];
self.datalabel=[[NSMutableArray alloc]init];
self.datavoice=[[NSMutableArray alloc]init];
self.dataimage=[[NSMutableArray alloc]init];
self.navigationController.navigationBar.barTintColor=RGB(120, 90, 184, 0.85);
//导航背景色
[self.navigationController.navigationBar setTitleTextAttributes:
@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont systemFontOfSize:20]}];
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
/*滚动方向*/ layout.scrollDirection=UICollectionViewScrollDirectionVertical;
//格子的大小
layout.itemSize = CGSizeMake(WIDTH/2-20, 200);
//行间距
layout.minimumLineSpacing = 5;
//列间距
layout.minimumInteritemSpacing = 10;
//网格视图 (表格 -> 需要注册,需要创建布局)
//1.frame
clV = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
clV.backgroundColor=[UIColor whiteColor];
//2.数据源和代理
clV.delegate = self;
clV.dataSource = self;
// 注册 cell
[clV registerClass:[VOCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
[self loadData];
//3.添加到主视图
[self.view addSubview:clV];
self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"➕" style:UIBarButtonItemStylePlain target:self action:@selector(click)];
}
-(void)loadData
{
AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
manager.responseSerializer=[AFHTTPResponseSerializer serializer];
[manager GET:@"http://123.126.40.109:7003/asmr/videos/A1100101.shtml" parameters:self progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:1 error:nil];
//整个的大数组加到字典里然后再给resultarr数组
NSArray *resultarr=dict[@"result"];
for (NSDictionary *dic in resultarr) {
Model *model=[Model new];
//把字典给数组
[self.dataimage addObject:dic[@"imageUrl"]];//这两个是一组
[self.datalabel addObject:dic[@"fileDescribe"]];//
// [self.datalabel addObject:dic[@"fileUrl"]];
[model setValuesForKeysWithDictionary:dic];
[self.datavoice addObject:model];//3这两个是一种
[self.dataSource addObject:model];//3
}
[self performSelectorOnMainThread:@selector(reloadTable) withObject:nil waitUntilDone:NO];
NSLog(@"网络请求 成功");
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"解析失败");
}];
}
-(void)reloadTable
{
[clV reloadData];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.dataSource.count;
}
//分区单元格
-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
VOCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
[cell.imagev sd_setImageWithURL:[NSURL URLWithString:self.dataimage[indexPath.row]] ];
cell.labelI.text=self.datalabel[indexPath.row];
return cell;
}
//点击单元格跳转
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[clV deselectItemAtIndexPath:indexPath animated:NO];
videodetailViewController *detail=[[videodetailViewController alloc]init];
Model *model=self.datavoice[indexPath.row];//1
detail.MP4url=model.fileUrl;//1
// detail.MP4url=self.str;//2
// self.str=self.datavoice[indexPath.row];//2
detail.hidesBottomBarWhenPushed=YES;
[self.navigationController pushViewController:detail animated:YES];
}
-(void)click
{
if (self.i==1) {
view=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width-170, 80, 150, 100)];
view.backgroundColor=[UIColor blackColor];
view.alpha=0.78;
for (int j=0; j<2; j++) {
imgI=[[UIImageView alloc]initWithFrame:CGRectMake(13,20+j*40, 30, 30)];
NSArray *arr = @[@"我的视频",@"我的音频"];
NSArray *arr1 = @[@"上传视频1",@"上传音频1"];
imgI.image = [UIImage imageNamed:arr[j]];
[view addSubview:imgI];
btn=[[UIButton alloc]initWithFrame:CGRectMake(25, j*40+20, 150, 30)];
[btn setTitle:arr1[j] forState:UIControlStateNormal];
[view addSubview:btn];
// btn.tag=i+1; tag唯一性 不等于1
//
}
[self.view addSubview:view];
self.i=0;//运行到这里停止
}else
{
view.frame=CGRectMake(0, 0, 0, 0);
imgI.frame=CGRectMake(0, 0, 0, 0);
btn.frame=CGRectMake(0, 0, 0, 0);
self.i=1;
}
}
@end
视频详情部分
videodetailViewController.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
NS_ASSUME_NONNULL_BEGIN
@interface videodetailViewController : MPMoviePlayerViewController//视频的
@property(nonatomic,strong)NSString *MP4url;
@end
NS_ASSUME_NONNULL_END
videodetailViewController.m
#import "videodetailViewController.h"
@implementation videodetailViewController
-(void)viewDidLoad
{
[super viewDidLoad];
self.moviePlayer.contentURL=[NSURL URLWithString:self.MP4url];
[self.moviePlayer play];
[self creatbtn];
}
-(void)creatbtn{
UIButton *btn =[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 50)];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(pushBack) forControlEvents:UIControlEventTouchUpInside];
}
-(void)pushBack{
[self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillAppear:(BOOL)animated
{
self.navigationController.navigationBar.hidden=YES;
}//进入时是否隐藏tabbar
-(void)viewDidAppear:(BOOL)animated
{
self.navigationController.navigationBar.hidden=NO;
}//退出时隐藏tabbar=显示tabbar
@end