//
// 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. 20145218 《Java程序设计》第10周学习总结

    20145218 <Java程序设计>第10周学习总结 教材学习内容总结 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据. 程序员所作的事情就是把数据发送到指定的位 ...

  2. servlet 配置

    <servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>com.web.s ...

  3. 委托传参,lambda

    今天一朋友问到一问题,怎么在timer的委托里面传参数,我初一想Invoke 可以传参数吧,我就说了. 后来他说这个委托是微软定义,不能变参数,于是我就自己写了一段代码试 试. 我最初写的代码如下 p ...

  4. Chrome plug-in 和Extension

    "扩展"和"插件",其实都是软件组件的一种形式,Chrome 只不过是把两种类型的组件分别给与了专有名称,一个叫"扩展",另一个叫" ...

  5. XHR2 和[FromBody]使用说明

    [FromBody]必须是application/json 否则会报415 不支持的类型 //Forms function FormsPost(data) { //Default Type x-www ...

  6. js控制html文字提示语的出现和隐藏

    有时我们需要在点击html输入框的时候,旁边会出现提示语.在输入字符的时候,输入框下边会出现输入了多少字符的提示. 请看下面实例. <!DOCTYPE html> <html> ...

  7. Asp.Net MVC3.0网站统计登录认证的在线人数

    Asp.Net MVC3.0网站统计登录认证的在线人数 前言 对于一个网站来说,统计在线人数是一个很重要的工作.平时也发现很多的网站论坛等都有在线人数的显示.对于一个网站如果在线人数很多,用户看到了这 ...

  8. TopCoder SRM 583 TurnOnLamps

    读错题了有没有呀,原来 lamps 是在边上的呀,当成是在点上的了,无语. 直接一个dfs 就可以 从叶子节点开始,如果有必要转换 lamp 的状态则加一个仅包含 这个 lamp 的段 然后向上扩展, ...

  9. HDU 3966 基础树链剖分

    题意:给一棵树,并给定各个点权的值,然后有3种操作:I C1 C2 K: 把C1与C2的路径上的所有点权值加上KD C1 C2 K:把C1与C2的路径上的所有点权值减去KQ C:查询节点编号为C的权值 ...

  10. 启用jboss热部署

    Please make sure to add             <configuration>                 <jsp-configuration deve ...