2016-1-15 抽屉效果实现demo
//
// 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的更多相关文章
- AJ学IOS(26)UI之iOS抽屉效果小Demo
AJ分享,必须精品 先看效果 实现过程 第一步,把三个view设置好,还有颜色 #warning 第一步 - (void)addChildView { // left UIView *leftView ...
- iOS側拉栏抽屉效果Demo
源代码下载 側拉栏抽屉效果Demo 须要导入第三方的类库例如以下: 抽屉效果所需第三方类库下载 效果:既能够两側都实现抽屉效果也可仅仅实现左側栏或者右側栏的抽屉效果 waterm ...
- 浅谈DrawerLayout(抽屉效果)
DrawerLayout是V4包下提供的一种左滑右滑抽屉布局效果. 实现效果如下: 因为是官方提供的,所以使用起来也相对的比较简单. DrawerLayout 提供 1.当界面弹出的时候,主要内容区会 ...
- 15款效果很酷的最新jQuery/CSS3特效
很久没来博客园发表文章了,今天就分享15款效果很酷的最新jQuery/CSS3特效,废话不说,一起来看看吧. 1.3D图片上下翻牌切换 一款基于jQuery+CSS3实现的3D图片上下翻牌切换效果,支 ...
- ios开发中超简单抽屉效果(MMDrawerController)的实现
ios开发中,展示类应用通常要用到抽屉效果,由于项目需要,本人找到一个demo,缩减掉一些不常用的功能,整理出一个较短的实例. 首先需要给工程添加第三方类库 MMDrawerController: 这 ...
- iOS中 超简单抽屉效果(MMDrawerController)的实现
ios开发中,展示类应用通常要用到抽屉效果,由于项目需要,本人找到一个demo,缩减掉一些不常用的功能,整理出一个较短的实例. 首先需要给工程添加第三方类库 MMDrawerController: 这 ...
- Wpf 抽屉效果
在android开发中有抽屉效果,就是在页面的边上有一个按钮,可以通过点击或者拖拽这个按钮,让页面显示.Wpf也可以实现相同的效果. 主要是通过一个DoubleAnimation和RectAnimat ...
- 基于Qt的相似QQ好友列表抽屉效果的实现
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/shuideyidi/article/details/30619167 前段时间在忙毕业设计, ...
- iOS开发之抽屉效果实现
说道抽屉效果在iOS中比较有名的第三方类库就是PPRevealSideViewController.一说到第三方类库就自然而然的想到我们的CocoaPods,今天的博客中用CocoaPods引入PPR ...
随机推荐
- angular 模板 小例子
参考网站:https://docs.angularjs.org/tutorial/step_09 先看下目录结构 新建个空文件夹, 输入命令: express --view ejs cnpm inst ...
- 关于iOS7 设计师需要了解的十件事
在今年的WWDC上,苹果推出了采用全新设计语言打造的iOS7.新系统弃用了诸如皮革.木质一类的伪3D拟真效果,取而代之的是更加简洁轻量的设计路线,其中文字排版成了重头戏,另外在某些方面也受到了扁平化设 ...
- [Eclipse] 详细设置护眼背景色和字体颜色并导出
http://jingyan.baidu.com/article/d5a880eb6c4f7813f147ccef.html Eclipse是一款码农们喜闻乐见的集成开发平台,但是其默认的主题和惨白的 ...
- linux----关于定位和查找
1.top --查看进程2.su --临时切换用户命令[root@tomato2 ~]# sudo su gongxijun[gongxijun@tomato2 root]$ 3.whoami --- ...
- hduoj----1142A Walk Through the Forest(记忆化搜索+最短路)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- node 日志管理log4js
node 日志管理log4js 一.默认的控制台输出 我们使用express框架时,开发模式用node或者supervisor启动nodejs应用时,控制台都是显示如下的日志. GET /css/bo ...
- 读《程序员的SQL金典》[1]--基础数据检索
前言 <程序员的SQL金典>这本书是杨中科老师的,拜读了一下,简单做了读书笔记供以后翻阅.仅供学习分享,要想细读的话推荐购买原版呀! 这次读书的时候用了新的办法把看书计划进行了量化,虽然简 ...
- BZOJ3307 雨天的尾巴
首先考虑序列怎么做... 只要把操作差分了,记录在每个点上 然后维护一棵权值线段树,表示每个颜色出现的次数,支持单点修改和查询最大值操作 只要把序列扫一遍就好了,时间复杂度$O(n + m*logZ) ...
- 怎么设置 mysql 多主复制
更新 其实本文主要来自www.digitalocean.com ,但是我没有买他们家的 VPS 用来 demo 了.只是用vagrant 来模拟了. 介绍 说说关于通过两台 vps 来扩展 mysql ...
- 使用ASP.Net WebAPI构建REST服务(二)——路由
REST并没有像传统的RPC服务那样显式指定了服务器函数的访问路径,而是将URL根据一定的规则映射为服务函数入口,这个规则就称之为路由.Asp.Net WebAPI的路由方式和Asp.Net MVC是 ...