//
// ViewController.m
// 抽屉
//
// Created by Mac on 16/1/15.
// Copyright © 2016年 Mac. All rights reserved.
// #import "ViewController.h" @interface ViewController ()
@property (nonatomic, weak) UIView *mainView;
@property (nonatomic, weak) UIView *leftView;
@property (nonatomic, weak) UIView *rightView;
@property (nonatomic, assign) BOOL drag; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
CGFloat scH = [UIScreen mainScreen].bounds.size.height;
CGFloat scW = [UIScreen mainScreen].bounds.size.width; UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(, , scW, scH)];
leftView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:leftView];
self.leftView = leftView; UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(, , scW, scH)];
rightView.backgroundColor = [UIColor redColor];
[self.view addSubview:rightView];
self.rightView = rightView; UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(, , scW, scH)];
mainView.backgroundColor = [UIColor grayColor];
[self.view addSubview:mainView];
self.mainView = mainView; UIPanGestureRecognizer *pan =[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self.mainView addGestureRecognizer:pan]; UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[self.view addGestureRecognizer:tap];
// [self.rightView addGestureRecognizer:tap];
// [self.leftView addGestureRecognizer:tap]; [self.rightView setHidden: YES];
[self.leftView setHidden:YES]; }
#pragma mark - Event after view clicked
- (void)tap:(UITapGestureRecognizer *)tapGest {
// UIView *view = tapGest.view;
NSLog(@"%s",__func__);
NSLog(@"%lf",self.mainView.frame.origin.x);
CGFloat scH = [UIScreen mainScreen].bounds.size.height;
CGFloat scW = [UIScreen mainScreen].bounds.size.width;
if (self.mainView.frame.origin.x != ) {
[UIView animateWithDuration:0.35 animations:^{
self.mainView.frame = CGRectMake(, , scW, scH);
} completion:^(BOOL finished) {
[self.rightView setHidden:YES];
[self.leftView setHidden:YES];
}];
}
}
- (void)pan:(UIPanGestureRecognizer *)panGest
{
CGFloat scH = [UIScreen mainScreen].bounds.size.height;
CGFloat scW = [UIScreen mainScreen].bounds.size.width; CGPoint transform = [panGest translationInView:panGest.view]; if (transform.x > ) {//向右滑动
if (self.leftView.isHidden) {//判断起始滑动的状态
[self.rightView setHidden:NO];
self.drag = NO;
[self setupFrameWith:self.drag and:panGest];
}
if(self.rightView.isHidden && self.mainView.frame.origin.x <= ){// 表明用户现在在左边的界面,但是需要往回拖,也需要注意self.mainView.origin.x的大小,以防用户拖过界
self.drag = YES;
[self setupFrameWith:self.drag and:panGest];
} }else if (transform .x < ){//向左滑动
if (self.rightView.isHidden) {
[self.leftView setHidden:NO];
self.drag = YES;
[self setupFrameWith:self.drag and:panGest];
}else if (self.leftView.isHidden&&self.mainView.frame.origin.x >= ){//现在背景是rightView,且需要判断self.mianView.frame.origin.x的值,以防用户拖过界
self.drag = NO;
[self setupFrameWith:self.drag and:panGest]; }
}
if (panGest.state == UIGestureRecognizerStateEnded) {// 表示手势结束
// 先判断现在主界面的位置,然后再决定是否隐藏界面和弹回界面 // 找出最大最小的x
CGFloat maxX = CGRectGetMaxX(self.mainView.frame);
CGFloat minX = CGRectGetMinX(self.mainView.frame);
NSLog(@"%lf",minX); // 用户拖过时弹回
if ( minX > 0.8*scW) {
NSLog(@"%s",__func__);
[UIView animateWithDuration:0.2 animations:^{
self.mainView.frame = CGRectMake(0.8*scW, 0.4*scW, scW, scH - 0.8*scW);
} ];
}
if (maxX < 0.2*scW) {
// NSLog(@"%s",__func__);
[UIView animateWithDuration:0.2 animations:^{
self.mainView.frame = CGRectMake(-0.8*scW, 0.4*scW, scW, scH - 0.8*scW);
} ];
} if ( minX < scW / && minX > ) {//此时在右界面且需要弹回 [UIView animateWithDuration:0.35 animations:^{
self.mainView.frame = CGRectMake(, , scW, scH);
} completion:^(BOOL finished) {
[self.rightView setHidden:YES];// 再动画完成后在隐藏;
}]; }else if(maxX > scW / && minX < ){
[UIView animateWithDuration:0.35 animations:^{
self.mainView.frame = CGRectMake(, , scW, scH);
} completion:^(BOOL finished) {
[self.leftView setHidden:YES];// 再动画完成后在隐藏;
}];
} } }
- (void)setupFrameWith:(BOOL)drag and:(UIPanGestureRecognizer *)panGest
{
CGFloat scH = [UIScreen mainScreen].bounds.size.height;
CGFloat scW = [UIScreen mainScreen].bounds.size.width;
// 原始frame
CGRect frame = self.mainView.frame; CGPoint transform = [panGest translationInView:panGest.view];
CGFloat x = frame.origin.x+ transform.x;
CGFloat y = frame.origin.y + transform.x/;
if(drag == YES){
x = frame.origin.x+ transform.x;
y = frame.origin.y - transform.x/;
} CGRect nextFrame = CGRectMake(x, y, scW, scH - y*); self.mainView.frame = nextFrame; [panGest setTranslation:CGPointZero inView:panGest.view]; }
@end

从这个demo里学习巩固了很多知识,感觉好爽~~

2016-1-15 抽屉效果实现demo的更多相关文章

  1. AJ学IOS(26)UI之iOS抽屉效果小Demo

    AJ分享,必须精品 先看效果 实现过程 第一步,把三个view设置好,还有颜色 #warning 第一步 - (void)addChildView { // left UIView *leftView ...

  2. iOS側拉栏抽屉效果Demo

    源代码下载 側拉栏抽屉效果Demo  须要导入第三方的类库例如以下: 抽屉效果所需第三方类库下载 效果:既能够两側都实现抽屉效果也可仅仅实现左側栏或者右側栏的抽屉效果           waterm ...

  3. 浅谈DrawerLayout(抽屉效果)

    DrawerLayout是V4包下提供的一种左滑右滑抽屉布局效果. 实现效果如下: 因为是官方提供的,所以使用起来也相对的比较简单. DrawerLayout 提供 1.当界面弹出的时候,主要内容区会 ...

  4. 15款效果很酷的最新jQuery/CSS3特效

    很久没来博客园发表文章了,今天就分享15款效果很酷的最新jQuery/CSS3特效,废话不说,一起来看看吧. 1.3D图片上下翻牌切换 一款基于jQuery+CSS3实现的3D图片上下翻牌切换效果,支 ...

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

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

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

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

  7. Wpf 抽屉效果

    在android开发中有抽屉效果,就是在页面的边上有一个按钮,可以通过点击或者拖拽这个按钮,让页面显示.Wpf也可以实现相同的效果. 主要是通过一个DoubleAnimation和RectAnimat ...

  8. 基于Qt的相似QQ好友列表抽屉效果的实现

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/shuideyidi/article/details/30619167     前段时间在忙毕业设计, ...

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

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

随机推荐

  1. ArcMap中"开始编辑"遇到一个或多个带有警告的图层“如果继续,可能无法编辑某些图层”的警告框

    开始编辑后可能出现的错误: 如果 ArcMap 在所选数据上启动编辑会话时遇到问题,将弹出一个对话框以提供附加信息.您可能会收到错误.警告或信息消息. 出现错误  时用户不可以启动任何编辑会话.只有解 ...

  2. centos7 学习

    1.centos pid文件: 在Linux系统的目录/var/run下面一般我们都会看到很多的*.pid文件.而且往往新安装的程序在运行后也会在/var/run目录下面产生自己的pid文件.那么这些 ...

  3. Windows NT访问权限

    #define SECTION_QUERY 0x0001 #define SECTION_MAP_WRITE 0x0002 #define SECTION_MAP_READ 0x0004 #defin ...

  4. hdu------1281 棋盘游戏(最小覆盖点)

    棋盘游戏 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  5. tableview调用reloadData()之后界面不刷新显示

    解决方法: 查看是否有指定tableView的delegate和datasource. self.tableView.delegate = self self.tableView.datasource ...

  6. 【如何快速的开发一个完整的iOS直播app】(推流篇)

    前言 在看这篇之前,如果您还不了解直播原理,请查看这篇文章如何快速的开发一个完整的iOS直播app(原理篇) 开发一款直播app,肯定需要流媒体服务器,本篇主要讲解直播中流媒体服务器搭建,并且讲解了如 ...

  7. tar.xz如何解压:linux和windows下tar.xz解压命令介绍

    在linux下怎么解压和压缩tar.xz文件? (本文由www.169it.com搜集整理) 在linux下解压tar.xz文件步骤 1 2 # xz -d ***.tar.xz  //先解压xz # ...

  8. SecureCRT从本传相片到服务器的注意事项

    rz -y -be 注意,要加上参数be

  9. Storm(3) - Calculating Term Importance with Trident

    Creating a URL stream using a Twitter filter Start by creating the project directory and standard Ma ...

  10. Linux系统中的日常监控知识点

    1.命令熟悉之w [xiongchao@oc3006745124 Desktop]$ w :: up :, users, load average: 1.48, 1.19, 1.11 USER TTY ...