1.导入第三方
2.创建 JDmodel继承于NSObject
在JDmodel.h:
//图片
@property(nonatomic, strong)NSArray *imgarr;
//主标题
@property(nonatomic, strong)NSArray *zlbarr;
//副标题
@property(nonatomic,strong)NSArray *flbarr;
//价格
@property(nonatomic,strong)NSArray *pricearr;
在JDmodel.m:
#import “JDmodel.h”
@implementation JDmodel
//图片
-(NSArray *)imgarr{
_imgarr=@[@"2",@"3",@"2",@"3",@"2",@"3"];
return _imgarr;
}
//主标题
-(NSArray *)zlbarr{
_zlbarr=@[@"物美价廉欢迎抢购?",@"物美价廉欢迎抢购?",@"物美价廉欢迎抢购?",@"物美价廉欢迎抢购?",@"物美价廉欢迎抢购?",@"物美价廉欢迎抢购?"];
return _zlbarr;
}
//副标题
-(NSArray *)flbarr{
_flbarr =@[@"活动还有三天结束",@"活动还有三天结束",@"活动还有三天结束",@"活动还有三天结束",@"活动还有三天结束",@"活动还有三天结束"];
return _flbarr;
}
//价格
-(NSArray *)pricearr{
_pricearr =@[@"100000.00?",@"100000.00?",@"100000.00?",@"100000.00?",@"100000.00?",@"100000.00?"];
return _pricearr;
}
@end
3.创建JDCollectionViewCell继承于UICollectionViewCell
在JDCollectionViewCell.h中:
@property(nonatomic,strong)UIImageView *img;
@property(nonatomic,strong)UILabel *zlb;
@property(nonatomic,strong)UILabel *flb;
@property(nonatomic,strong)UILabel *price;
在JDCollectionViewCell.m中:
#import “JDCollectionViewCell.h”
@implementation JDCollectionViewCell
-(instancetype)initWithFrame:(CGRect)frame{
if (self =[super initWithFrame:frame]) {
[self addSubview:self.img];
[self addSubview:self.zlb];
[self addSubview:self.flb];
[self addSubview:self.price];
}
return self;
}
-(UIImageView *)img{
if (!_img) {
_img=[[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 200, 200)];
}
return _img;
}
-(UILabel *)zlb{
if (!_zlb) {
_zlb =[[UILabel alloc]initWithFrame:CGRectMake(10,210,200,30)];
_zlb.font=[UIFont systemFontOfSize:16];
_zlb.textColor=[UIColor redColor];
_zlb.numberOfLines=0;
}
return _zlb;
}
-(UILabel *)flb{
if (!_flb) {
}
return _flb;
}
-(UILabel *)price{
if (!_price) {
}
return _price;
}
@end
4.ViewController继承改成UITabBarController
在ViewController.m中:
#import “ViewController.h”
#import “oneViewController.h”
#import “twoViewController.h”
#import “threeViewController.h”
#import “fourViewController.h”
@interface ViewController ()
@end
@implementation ViewController
-(void)viewDidLoad {
[super viewDidLoad];
oneViewController *one =[oneViewController new];
UINavigationController *onev =[[UINavigationController alloc]initWithRootViewController:one];
onev.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"首页" image:[UIImage imageNamed:@""] selectedImage:[UIImage imageNamed:@""]];
twoViewController *two =[twoViewController new];
UINavigationController *twov =[[UINavigationController alloc]initWithRootViewController:two];
twov.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"商城" image:[UIImage imageNamed:@""] selectedImage:[UIImage imageNamed:@""]];
threeViewController *three =[threeViewController new];
UINavigationController *threev =[[UINavigationController alloc]initWithRootViewController:three];
threev.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"收藏" image:[UIImage imageNamed:@""] selectedImage:[UIImage imageNamed:@""]];
fourViewController *four =[fourViewController new];
UINavigationController *fourv =[[UINavigationController alloc]initWithRootViewController:four];
fourv.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"未登录" image:[UIImage imageNamed:@""] selectedImage:[UIImage imageNamed:@""]];
self.viewControllers=@[onev,twov,threev,fourv];
}
@end
5.创建oneViewController,twoViewController,threeViewController,fourViewController继承于UIViewController
在oneViewController.m中:
#import “oneViewController.h”
#import “FNViewController.h”
#import “FDViewController.h”
#import “SCNavTabBarController.h”
@interface oneViewController ()
@end
@implementation oneViewController
-(void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor =[UIColor whiteColor];
//分段控制器
FNViewController *fn =[FNViewController new];
fn.title=@"全部";
FDViewController *fd =[FDViewController new];
fd.title=@"精选";
// 创建SCNavTabBar
SCNavTabBarController *scNav = [SCNavTabBarController new];
// scNav.d
scNav.subViewControllers = @[fn , fd];
// 设置背景颜色
[scNav setNavTabBarColor:[UIColor whiteColor]];
// 执行管理
[scNav addParentController:self];
//左右按钮
UIBarButtonItem *left =[[UIBarButtonItem alloc]initWithTitle:@"扫码" style:UIBarButtonItemStyleDone target:self action:nil];
UIBarButtonItem *right =[[UIBarButtonItem alloc]initWithTitle:@"?" style:UIBarButtonItemStyleDone target:self action:nil];
//添加到导航标题上
self.navigationItem.leftBarButtonItem=left;
self.navigationItem.rightBarButtonItem=right;
//添加搜索条
UISearchBar *sb =[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 180, 40)];
sb.placeholder=@"挑好物上京东";
self.navigationItem.titleView=sb;
}
@end
6.在twoViewController,threeViewController,fourViewController的.h中修改背景颜色。
self.view.backgroundColor =[UIColor grayColor];
7.创建FNViewController,FDViewController,nextViewController继承于
UIViewController
在FNViewController.m中:
网格:
#import “FNViewController.h”
#import “JDCollectionViewCell.h”
#import “JDmodel.h”
#import “nextViewController.h”
@interface FNViewController ()<UICollectionViewDelegate , UICollectionViewDataSource>
{
UICollectionViewFlowLayout *flowLayout;
UICollectionView *cv;
}
@end
static NSString *oj = @“cell”;
@implementation FNViewController
-(void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor whiteColor];
// 创建流水布局
flowLayout = [[UICollectionViewFlowLayout alloc]init];
// 设置网格大小
flowLayout.itemSize = CGSizeMake((self.view.frame.size.width-10)/2, 320);
// 设置最小行间距
flowLayout.minimumLineSpacing = 0;
// 设置最小列间距
flowLayout.minimumInteritemSpacing = 0;
// 创建网格
cv = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
// 设置代理
cv.delegate = self;
cv.dataSource = self;
// 设置网格背景颜色
cv.backgroundColor = [UIColor whiteColor];
// 注册cell
[cv registerClass:[JDCollectionViewCell class] forCellWithReuseIdentifier:oj];
[self.view addSubview:cv];
}
// 设置网格个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 6;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
JDCollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:oj forIndexPath:indexPath];
//初始化model
JDmodel *model =[JDmodel new];
cell.img.image=[UIImage imageNamed:model.imgarr[indexPath.row]];
cell.zlb.text=model.zlbarr[indexPath.row];
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
self.hidesBottomBarWhenPushed =YES;
nextViewController *vc =[nextViewController new];
[self.navigationController pushViewController:vc animated:YES];
self.hidesBottomBarWhenPushed = NO;
}
//隐藏导航
-(void)viewWillAppear:(BOOL)animated{
self.navigationController.navigationBar.hidden = NO;
}
@end
8.在FDViewController.m中添加背景颜色:self.view.backgroundColor=[UIColor orangeColor];
9.在nextViewController.m中:
#import “nextViewController.h”
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
@interface nextViewController ()
@end
@implementation nextViewController
-(void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor whiteColor];
UIImageView *imgV = [[UIImageView alloc]initWithFrame:self.view.frame];
imgV.image = [UIImage imageNamed:@"4.jpg"];
[self.view addSubview:imgV];
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(20, 50, 40, 40)];
btn.backgroundColor=[UIColor cyanColor];
[btn setTitle:@"返回" forState:UIControlStateNormal];
btn.layer.masksToBounds=YES;
btn.layer.cornerRadius=20;
[btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake((self.view.frame.size.width-100)/2, 300, 100, 100)];
btn1.backgroundColor=[UIColor cyanColor];
[btn1 setTitle:@"播放" forState:UIControlStateNormal];
btn1.layer.masksToBounds=YES;
btn1.layer.cornerRadius=50;
[btn1 addTarget:self action:@selector(playbtn) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
}
-(void)playbtn{
// 本地资源文件
NSString *filePath = [[NSBundle mainBundle]pathForResource:@“2.mp4” ofType:nil];
// 创建视频播放控制器
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc]init];
// 设置视频播放器
playerViewController.player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:filePath]];
[self presentViewController:playerViewController animated:YES completion:nil];
// 4. 开始播放 : 默认不会自动播放
[playerViewController.player play];
}
-(void)click{
[self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillAppear:(BOOL)animated{
self.navigationController.navigationBar.hidden = YES;
}
@end