#import "ViewController.h"
#import "TwoViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
UITableView *tab;
UIView *leftView;
UIView *mainView;
NSArray *arr;
BOOL isSelect;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//设置导航条标题
self.title = @"Main";
//设置左侧按钮
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"left" style:UIBarButtonItemStyleDone target:self action:@selector(click)];
self.navigationItem.leftBarButtonItem = leftItem;
//初始化数组
arr = @[@"?个性用户表情",@"?QQ钱包",@"个性装扮",@"我的收藏"];
//创建左侧视图
leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, [UIScreen mainScreen].bounds.size.height)];
//添加到视图
[self.view addSubview:leftView];
//创建主视图
mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height)];
mainView.backgroundColor = [UIColor whiteColor];
//添加到视图
[self.view addSubview:mainView];
//创建图片框
UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
//设置图片
imgV.image = [UIImage imageNamed:@"2.png"];
//将图片框添加到左侧视图
[leftView addSubview:imgV];
//创建表格
tab = [[UITableView alloc] initWithFrame:CGRectMake(0, 300, 300, self.view.frame.size.height) style:UITableViewStylePlain];
//取消垂直滚动条
tab.showsVerticalScrollIndicator = NO;
//设置代理
tab.delegate = self;
//设置数据源
tab.dataSource = self;
//将表格添加到左侧view
[leftView addSubview:tab];
}
//数据源方法
//设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return arr.count;
}
//设置cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//创建可重用标识符
static NSString *ID = @"cell";
//通过可重用标识符查找
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//判断
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
//设置内容
cell.textLabel.text = arr[indexPath.row];
//返回cell
return cell;
}
//点击左侧按钮方法
-(void)click{
if (isSelect == NO) {
// (NSTimeInterval) 时间间隔
[UIView animateWithDuration:0.5 animations:^{
//移动视图
self.navigationController.navigationBar.transform = CGAffineTransformMakeTranslation(300, 0);
//移动MainView
mainView.transform = CGAffineTransformMakeTranslation(300, 0);
}];
isSelect = YES;
}else{
//恢复原位
[UIView animateWithDuration:0.5 animations:^{
//恢复原位 导航条
self.navigationController.navigationBar.transform = CGAffineTransformIdentity;
//恢复leftView
leftView.transform = CGAffineTransformIdentity;
//恢复mainView
mainView.transform = CGAffineTransformIdentity;
}];
isSelect = NO;
}
}
//点击屏幕的方法
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[UIView animateWithDuration:0.5 animations:^{
//恢复原位 导航条
self.navigationController.navigationBar.transform = CGAffineTransformIdentity;
//恢复leftView
leftView.transform = CGAffineTransformIdentity;
//恢复mainView
mainView.transform = CGAffineTransformIdentity;
}];
isSelect = NO;
}
//点击单元格的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[UIView animateWithDuration:0.5 animations:^{
//恢复原位 导航条
self.navigationController.navigationBar.transform = CGAffineTransformIdentity;
//恢复leftView
leftView.transform = CGAffineTransformIdentity;
//恢复mainView
mainView.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
//创建第二个控制器
TwoViewController *twoVc = [[TwoViewController alloc] init];
[self.navigationController pushViewController:twoVc animated:YES];
}];
isSelect = NO;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end