效果图如下

  • 代码实现以及思路下面分析:
  • 代码创建导航控制器
  • 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"];
}
  • 注意:注册cell的同时调用了 self.tableView 则调用了懒加载,此时tableView已经创建了.必须要先创建,否则有一个小bug就是,当tableView第一次弹出的时候会从屏幕的(0,0)点弹出,而不是整个tableView从左侧弹出.

#pragma mark - 初始化侧栏按钮
-(void)initLeftBarButton{ UIButton * btnLeft = [[UIButton alloc] init];
btnLeft.frame = CGRectMake(0, 0, 90, 40);
[btnLeft setTitle:@"侧栏" forState:UIControlStateNormal];
[btnLeft setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnLeft addTarget:self action:@selector(didLeftBtn) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnLeft];
self.btnLeft = self.navigationItem.leftBarButtonItem;
} #pragma mark - 懒加载tableView
-(UITableView *)tableView{ if (_tableView == nil) {
_tableView = [[UITableView alloc] init];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.backgroundColor = [UIColor orangeColor];
//第一次点击tableView从左上角弹出,优化方案--先创建出tableView
CGFloat hight = kScreenH;
CGFloat x = 0;
CGFloat y = kNavW;
CGFloat width = 0;
_tableView.frame = CGRectMake(x, y, width, hight);
//取消显示竖直滚动条
_tableView.showsVerticalScrollIndicator = NO;
}
return _tableView;
}
  • 懒加载的时候直接创建tableView,让其宽度 == 0 即可.

#pragma mark - 点击侧栏按钮弹出tableView
-(void)didLeftBtn{ //禁用button等待动画执行完毕再启用button
self.btnLeft.enabled = NO;
CGFloat hight = kScreenH;
CGFloat x = 0;
CGFloat y = kNavW;
if (!self.openSlide) {
//添加动画
[UIView animateWithDuration:0.3 animations:^{
CGFloat width = kScreenW / 3;
self.tableView.frame = CGRectMake(x, y, width, hight);
}];
[self.view addSubview:self.tableView];
} else {
[UIView animateWithDuration:0.3 animations:^{
CGFloat width = 0;
self.tableView.frame = CGRectMake(x, y, width, hight);
}];
}
//执行完毕动画 取消禁用button
[self performSelector:@selector(setBtnLeftEnabled) withObject:nil afterDelay:0.3];
//监视侧栏是否打开
if (self.openSlide == YES) {
self.openSlide = NO;
} else {
self.openSlide = YES;
}
}
  • 点击 侧栏 按钮弹出tableView,此过程中让其动画执行,不会显得生硬.让tableView的宽度从0---> 屏幕宽度的三分之一
  • 记录tableView打开的状态.
  • 执行动画的过程中禁用 侧栏 按钮,由于代码执行时间的瞬间完成的,动画执行时间是0.3s,则延迟0.3s取消禁用 侧栏 按钮.

//不用反复创建tableView
//#pragma mark - 移除tableView
//-(void)removeSliedView{
//
// [self.tableView removeFromSuperview];
// self.btnLeft.enabled = YES;
//}
#pragma mark - 动画执行完毕启用"侧栏"按钮
-(void)setBtnLeftEnabled{ self.btnLeft.enabled = YES;
//动画执行完毕让第一个cell显示在最顶端
self.tableView.contentOffset = CGPointMake(0, 0);
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
  • 之前犯过一个错误就是点击 侧栏 按钮创建tableView,再点击 销毁 tableView,这样比较耗性能.通过懒加载先创建tableView,收回tableView的时候让其宽度 == 0 即可.

  • 上图演示的可以看出,当滑动tableView的时候,再次点击进去tableView还是滑动的位置,不会恢复到开始 下标为 0 的cell为最上面显示的cell.优化方案:让tableView的偏移contentOffset等于 0即可.代码不能写在 弹出tableView 与 收回 tableView的动画代码中,因为这样会让人看出来.写在动画执行完毕后的代码中.

  • 源代码地址:https://git.oschina.net/alexpei/PBSliedMenu.git

ios每日一发--仿侧边抽屉效果的更多相关文章

  1. ios开发中超简单抽屉效果(MMDrawerController)的实现

    ios开发中,展示类应用通常要用到抽屉效果,由于项目需要,本人找到一个demo,缩减掉一些不常用的功能,整理出一个较短的实例. 首先需要给工程添加第三方类库 MMDrawerController: 这 ...

  2. ios每日一发--Leanclude数据云存储以及登录 注册账户

    利用LeanCloud来实现注册账号,存储账号以及,登录时查询账号是否正确.集成方式很简单可以看这里的官方文档.地址是这里: https://leancloud.cn/docs/ 在这里创建应用,以及 ...

  3. iOS开发之抽屉效果实现

    说道抽屉效果在iOS中比较有名的第三方类库就是PPRevealSideViewController.一说到第三方类库就自然而然的想到我们的CocoaPods,今天的博客中用CocoaPods引入PPR ...

  4. 玩转iOS开发 - 简易的实现2种抽屉效果

    BeautyDrawer BeautyDrawer 是一款简单易用的抽屉效果实现框架,集成的属性能够对view 滑动缩放进行控制. Main features 三个视图,主视图能够左右滑动.实现抽屉效 ...

  5. iOS详解MMDrawerController抽屉效果(一)

      提前说好,本文绝对不是教你如何使用MMDrawerController这个第三方库,因为那太多人写了 ,也太简单了.这篇文章主要带你分析MMDrawerController是怎么实现抽屉效果,明白 ...

  6. iOS中 超简单抽屉效果(MMDrawerController)的实现

    ios开发中,展示类应用通常要用到抽屉效果,由于项目需要,本人找到一个demo,缩减掉一些不常用的功能,整理出一个较短的实例. 首先需要给工程添加第三方类库 MMDrawerController: 这 ...

  7. iOS仿支付宝首页效果

    代码地址如下:http://www.demodashi.com/demo/12776.html 首先看一下效果 状态栏红色是因为使用手机录屏的原因. 1.问题分析 1.导航栏A有两组控件,随着tabl ...

  8. iOS实现抽屉效果

    抽屉效果 在iOS中非常多应用都用到了抽屉效果,比如腾讯的QQ,百度贴吧- --- 1. 终于效果例如以下图所看到的 --- 2.实现步骤 1.開始启动的时候.新建3个不同颜色的View的 1.设置3 ...

  9. iOS开发——实用技术OC篇&简单抽屉效果的实现

    简单抽屉效果的实现 就目前大部分App来说基本上都有关于抽屉效果的实现,比如QQ/微信等.所以,今天我们就来简单的实现一下.当然如果你想你的效果更好或者是封装成一个到哪里都能用的工具类,那就还需要下一 ...

随机推荐

  1. 【BZOJ】2172: Mario填格子

    题意 \(3 * 3\)的网格,给出左上角的数字\(m\)和右下角的数字\(m\),如果当前格子有数字\(x\),格子左边有个数字\(y\),格子上面有个数字\(z\),则\(y|x, z|x\).格 ...

  2. 【POJ】2096 Collecting Bugs

    http://poj.org/problem?id=2096 题意:s个系统n种bug,每天找出一个bug,种类的概率是1/n,系统的概率是1/s.问:每个系统至少找出一个bug:每种类的bug都被找 ...

  3. [BZOJ2794][Poi2012]Cloakroom

    2794: [Poi2012]Cloakroom Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 167  Solved: 119[Submit][St ...

  4. linux 下如何打开core dump文件开关

    dump文件可以在程序crash时,方便我们查看程序crash的地方和上下文信息.在window下,要能生成dump文件,需要自己编写相应的代码.不过现在网上可以找到相应的代码,只要把它下载后然后加到 ...

  5. Java_关于App class loader的总结

    Java本身是一种设计的非常简单,非常精巧的语言,所以Java背后的原理也很简单,归结起来就是两点: 1.JVM的内存管理 理解了这一点,所有和对象相关的问题统统都能解决 2.JVM Class Lo ...

  6. Mockito学习资料

    官网:http://mockito.org/ https://dzone.com/refcardz/mockito

  7. hadoop编程小技巧(5)---自定义输入文件格式类InputFormat

    Hadoop代码测试环境:Hadoop2.4 应用:在对数据需要进行一定条件的过滤和简单处理的时候可以使用自定义输入文件格式类. Hadoop内置的输入文件格式类有: 1)FileInputForma ...

  8. golang github.com/go-sql-driver/mysql 遇到的数据库,设置库设计不合理的解决方法

    golang github.com/go-sql-driver/mysql 遇到的数据库,设置库设计不合理的解决方法,查询中报了以下这个错 Scan error on column index 2: ...

  9. jQuery $(document).ready()和window.onload

    jQuery $(document).ready()和window.onload 根据ready()方法的API说明http://api.jquery.com/ready/. 这个方法接收一个func ...

  10. CSS3两个动画顺序衔接播放

    问题描述: 第一个动画先播放,播放完成后,第二个动画紧接着播放. 解决办法: 1. 将第二个的延迟时间(animation-delay) 设置成第一个的持续时间( animation-duration ...