//
// 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. quartz定时任务框架的使用

    quartz定时任务时间设置 这些星号由左到右按顺序代表 :     *    *     *     *    *     *   *                                 ...

  2. 33-Url辅助方法

    Url辅助方法与HTML辅助方法很类似,HTML辅助方法用来产生HTML标签,而Url辅助方法则负责用来产生Url网址. @Url.Action("About") 最后的输出网址如 ...

  3. avatar Logo

    用日志打印出自己的头像logo import java.io.File; import java.io.FileInputStream; import java.io.IOException; imp ...

  4. lucene Lock obtain timed out: Lock@

    出错界面: 解决办法: 出现以上异常主要有两种原因: 1.系统正在写索引未完成之前,应用程序关闭 解决方法:删除提示的lock文件后重启应用(最好在应用中捕捉到,自动删除) 2.系统中有多个线程或程序 ...

  5. Java 中equals和toString()方法重写

    1,equals方法 (1)什么时候需要重写? 如果希望不同内存但相同内容的两个对象equals时返回true,则需要重写equals (2)怎么重写? class A { public int i; ...

  6. [转]JDK6和JDK7中的substring()方法

    substring(int beginIndex, int endIndex)在JDK6与JDK7中的实现方式不一样,理解他们的差异有助于更好的使用它们.为了简单起见,下面所说的substring() ...

  7. PDF 补丁丁 0.4.1 版:新增嵌入中文字库、替换文档字库的功能

    PDF 补丁丁 0.4.1 版新增了嵌入中文字库.替换文档字库的功能. 嵌入汉字字库 历史上有一批黄底黑字的 PDF 文档.这批文档都具有相同的问题:没有嵌入字库.在一些设备上阅读时显示乱码.复制文本 ...

  8. 通过计算机名访问linux

    1.安装samba 2.设置/etc/samba/smb.conf的 netbois name 配置节的值为你要设置的名称,如 netbois name = mylinux 也可以不设置此项,如果不设 ...

  9. CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组

    题目链接:CF #365 (Div. 2) D - Mishka and Interesting sum 题意:给出n个数和m个询问,(1 ≤ n, m ≤ 1 000 000) ,问在每个区间里所有 ...

  10. call,apply,bind函数

    一.call函数 a.call(b); 简单的理解:把a对象的方法应用到b对象上(a里如果有this,会指向b) call()的用法:用在函数上面 var Dog=function(){ this.n ...