ios 点击按钮右侧滑出表格效果

AppDelegate

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
ViewController * vc = [[ViewController alloc] init];
//必须要初始化导航控制器的根控制器
UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];

ViewController.m

#import “ViewController.h”
#define kScreenH [UIScreen mainScreen].bounds.size.height
#define kScreenW [UIScreen mainScreen].bounds.size.width

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView tableView;
/
* 记录是否打开侧边栏 */
@property (nonatomic, assign) BOOL openSlide;

@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@“右侧栏” style:(UIBarButtonItemStylePlain) target:self action:@selector(rightAction)];

}
#pragma mark - 选中某个cell代理方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(@"%@",cell.textLabel.text);
//选中cell后立即取消选中
[tableView deselectRowAtIndexPath:indexPath animated:YES];

}

#pragma mark - tableView数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return 20;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * ID = @“cell”;
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@“我是%zd”,indexPath.row];
cell.backgroundColor = [UIColor orangeColor];
return cell;
}

#pragma mark - 懒加载tableView
-(UITableView *)tableView{

if (_tableView == nil) {
    _tableView = [[UITableView alloc] init];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor orangeColor];
    //注册cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    //第一次点击tableView从左上角弹出,优化方案--先创建出tableView
    CGFloat hight = kScreenH;
   
   
    CGFloat width = 0;
    _tableView.frame = CGRectMake(kScreenW, 0, width, hight);
    //取消显示竖直滚动条
    _tableView.showsVerticalScrollIndicator = NO;
}
return _tableView;

}
#pragma mark - 点击侧栏按钮弹出tableView
-(void)rightAction{

//禁用button等待动画执行完毕再启用button
self.navigationItem.rightBarButtonItem.enabled = NO;
CGFloat hight = kScreenH;

// CGFloat x = 0;
CGFloat y = 0;
if (!self.openSlide) {
[UIView animateWithDuration:0.3 animations:^{
CGFloat width = kScreenW / 2;
self.tableView.frame = CGRectMake(kScreenW/2, y, width, hight);
}];
[self.view addSubview:self.tableView];
} else {
[UIView animateWithDuration:0.3 animations:^{
CGFloat width = 0;
self.tableView.frame = CGRectMake(kScreenW, y, width, hight);
}];
}
//执行完毕动画 取消禁用button
[self performSelector:@selector(setBtnLeftEnabled) withObject:nil afterDelay:0.3];
//监视侧栏是否打开
if (self.openSlide == YES) {
self.openSlide = NO;
} else {
self.openSlide = YES;
}
}
#pragma mark - 动画执行完毕启用"侧栏"按钮
-(void)setBtnLeftEnabled{

self.navigationItem.rightBarButtonItem.enabled = YES;
//动画执行完毕让第一个cell显示在最顶端
self.tableView.contentOffset = CGPointMake(0, 0);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值