ios仿侧边抽屉效果实现代码

2020-01-15 14:30:22王振洲
这篇文章主要为大家详细介绍了ios仿侧边抽屉效果实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下  

效果图如下

ios,抽屉效果

代码实现以及思路下面分析:
代码创建导航控制器
Appdelegate.m中


#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

  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];
  return YES;
}

viewcontroller.m中


//
// ViewController.m
// PBSliedMenu
//
// Created by 裴波波 on 16/4/21.
// Copyright © 2016年 裴波波. All rights reserved.
//

#import "ViewController.h"
#define kScreenH [UIScreen mainScreen].bounds.size.height
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kNavW 64
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
/** 记录是否打开侧边栏 */
@property (nonatomic, assign) BOOL openSlide;
/** 侧栏按钮 */
@property (nonatomic, strong) UIBarButtonItem *btnLeft;

@end

用一个bool值来记录左侧view是打开还是关闭状态.每次点击都要改变记录tableView状态的值
用属性保存 侧栏 按钮,用来当左侧tableView正在弹出或者收回执行动画过程中禁用.


@implementation ViewController

#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;
}

- (void)viewDidLoad {
  
  [super viewDidLoad];
  self.view.backgroundColor = [UIColor whiteColor];
  [self initLeftBarButton];
  //注册cell
  [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}