//
// 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. 理解Cookie和Session机制

    转载: 理解Cookie和Session机制 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录 ...

  2. 二级菜单jquery

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Python--关于set

    慕课网<Pyrhon入门>学习笔记 1.set 特性 set 持有一系列元素,这一点和 list 很像,但是set的元素没有重复,而且是无序的,这点和 dict 的 key很像. 可以将s ...

  4. 小米域名过渡JS

    <script>jQuery(function($) { (function() { var $bnBar = $(''), $topBn = $('#J_topBn'), $topBnB ...

  5. hdu------(3549)Flow Problem(最大流(水体))

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...

  6. 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property ..

    解决办法是:关闭tomcat,双击eclipse下tomcat服务器,在出来的Tomcat server at localhost页面中找到server options选项,选中其中的选项" ...

  7. SAP本地文件策略(导EXCEL选择拒绝后处理)

    导出EXCEL意外选择了拒绝+记住选择,这样的话在本地电脑就导不出文件了,如下图: 解决办法有2个: 1,修改导出文件的本地策略 :Alt+F12 ->选项->安全性->安全设置-& ...

  8. 10 vi简介(重点)

    1.为什么学习vi? vi很多系统都预装,如果我们的系统没有图像界面,可以使用vi vi是轻量级且执行快速的编辑器 2.vi的几种模式 命令模式.插入模式.底行模式 1) 命令行模式(command ...

  9. Subgraph Search Over Large Graph Database

    Subgraph Search Over Large Graph Database Problem Definition Given a graph database and a query grap ...

  10. ZOJ 3822(求期望)

    Domination Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Judge Edward is the headm ...