抽屉效果

  • 创建一个新的控制器为cell的点击跳转界面
#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值