简单的iOS抽屉效果
#define screenW [UIScreen mainScreen].bounds.size.width
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong)UIView * redView;
@property (nonatomic,strong)UIView *yellowView;
@property (nonatomic,strong)UIView *greenView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpView];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
[self.view addGestureRecognizer:pan];
}
//根据当前最前面视图的frame确定是否隐藏第二个View
- (void)Hidden{
if (self.greenView.frame.origin.x>0) {//往右移动,显示黄色的View
self.yellowView.hidden = NO;
}else if (self.greenView.frame.origin.x<0){//往左移动,隐藏黄色的View
self.yellowView.hidden = YES;
}
}
#define rightLocation 275
#define leftLocation -250
#pragma mark pan的方法
- (void)pan:(UIPanGestureRecognizer *)pan{
//获取手势移动的位置
CGPoint point = [pan translationInView:self.view];
//获取X轴的偏移量
CGFloat offSetX = point.x;
//修改greenView的frame
self.greenView.frame = [self changeFrameWith:offSetX];
//判断greenViewdex是否大于0
[self Hidden];
//复位
[pan setTranslation:CGPointZero inView:self.view];
//判断下当手势结束的时候,定位
if (pan.state == UIGestureRecognizerStateEnded) {
CGFloat target = 0;
if (self.greenView.frame.origin.x > screenW * 0.5) {
target = rightLocation;
}else if (CGRectGetMaxX(self.greenView.frame)<screenW * 0.5){
target = leftLocation;
}
//获取偏移量
CGFloat offSetX = target-self.greenView.frame.origin.x;
[UIView animateWithDuration:0.25 animations:^{
self.greenView.frame = target == 0?self.view.bounds:[self changeFrameWith:offSetX];
}];
}
}
#define MaxY 100
//根据偏移量获取一个新的frame
#pragma mark - 根据offSetX计算greenView的frame
- (CGRect)changeFrameWith:(CGFloat)offSetX{
//获取上一次的frame
CGRect frame = self.greenView.frame;
//获取上一次的高和宽
CGFloat oldW = frame.size.width;
CGFloat oldH = frame.size.height;
//移动后的x
CGFloat curX =frame.origin.x + offSetX;
//y轴方向偏移量
CGFloat offsetY = offSetX*MaxY/screenW;
//当前的frame高度
CGFloat curH = frame.size.height - 2*offsetY;
if (frame.origin.x<0) {//往左移动
curH = oldH+2*offsetY;
}
//获取尺寸的缩放比例
CGFloat scale = curH/oldH;
CGFloat curW = oldW * scale;
CGFloat curY = (self.view.frame.size.height - curH)*0.5;
//更改frame
frame.origin.x = curX;
frame.origin.y = curY;
frame.size.width = curW;
frame.size.height = curH;
return frame;
}
#pragma mark 添加View
- (void)setUpView{
self.redView = [[UIView alloc]initWithFrame:self.view.frame];
self.redView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.redView];
self.yellowView = [[UIView alloc]initWithFrame:self.view.frame];
self.yellowView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:self.yellowView];
self.greenView = [[UIView alloc]initWithFrame:self.view.frame];
self.greenView.backgroundColor = [UIColor greenColor];
[self.view addSubview:self.greenView];
}
@end
简单的iOS抽屉效果的更多相关文章
- iOS开发——高级篇——iOS抽屉效果实现原理
实现一个简单的抽屉效果: 核心思想:KVO实现监听mainV的frame值的变化 核心代码: #import "ViewController.h" // @"frame& ...
- iOS抽屉效果
源代码下载 抽屉效果第三方类库下载 所需第三方类库下载 側拉栏抽屉效果图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTUhUaW9z/font/5a6L ...
- AJ学IOS(26)UI之iOS抽屉效果小Demo
AJ分享,必须精品 先看效果 实现过程 第一步,把三个view设置好,还有颜色 #warning 第一步 - (void)addChildView { // left UIView *leftView ...
- ios开发中超简单抽屉效果(MMDrawerController)的实现
ios开发中,展示类应用通常要用到抽屉效果,由于项目需要,本人找到一个demo,缩减掉一些不常用的功能,整理出一个较短的实例. 首先需要给工程添加第三方类库 MMDrawerController: 这 ...
- iOS开发——实用技术OC篇&简单抽屉效果的实现
简单抽屉效果的实现 就目前大部分App来说基本上都有关于抽屉效果的实现,比如QQ/微信等.所以,今天我们就来简单的实现一下.当然如果你想你的效果更好或者是封装成一个到哪里都能用的工具类,那就还需要下一 ...
- iOS中 超简单抽屉效果(MMDrawerController)的实现
ios开发中,展示类应用通常要用到抽屉效果,由于项目需要,本人找到一个demo,缩减掉一些不常用的功能,整理出一个较短的实例. 首先需要给工程添加第三方类库 MMDrawerController: 这 ...
- 玩转iOS开发 - 简易的实现2种抽屉效果
BeautyDrawer BeautyDrawer 是一款简单易用的抽屉效果实现框架,集成的属性能够对view 滑动缩放进行控制. Main features 三个视图,主视图能够左右滑动.实现抽屉效 ...
- iOS开发之抽屉效果实现
说道抽屉效果在iOS中比较有名的第三方类库就是PPRevealSideViewController.一说到第三方类库就自然而然的想到我们的CocoaPods,今天的博客中用CocoaPods引入PPR ...
- 更好的抽屉效果(ios)
昨天项目基本没啥事了,晚上早早的就回家了,躺在床上无聊地玩着手机(Android的),在清理系统垃圾时被一个“360手机助手”给吸引了, 其实我是被它的那个抽屉效果给吸引了,此时你也许会觉得我out了 ...
随机推荐
- CodeForces 452C Magic Trick (排列组合)
#include <iostream> #include <cstdio> #include<cmath> #include<algorithm> us ...
- FineUI框架 使用asp.net控件及其使用问题
FineUI 基于ExtJS的开源ASP.Net框架库--创建 No JavaScript,No CSS,No UpdatePanel,No ViewState,No WebServices 的网站应 ...
- myeclipse 配置weblogic 异常
java.lang.UnsupportedClassVersionError: Bad version number in .class file当前JDK与weblogic版本不匹配.
- ajax异步服务器获取时间
1.创建ajax对象 <script type="text/javascript"> //创建AJAX异步对象 function createAJAX(){ var a ...
- codeforces 620F. Xors on Segments
题目链接 定义一种操作f(u, v) = u^u+1^.......^v. (u<=v), 给n个数, q个询问, 每个询问给出一个区间[l, r], 求这个区间里的f(a[i], a[j]) ...
- 02-C语言执行过程
目录: 一.MACOS系统操作 二.C语言的使用方式 三.编码 四.编译 五.运行 六.分析第一个C程序 七.预处理指令#include 八.完整执行过程 回到顶部 一.MACOS系统操作 操作计算机 ...
- Beat It
They Told Him他们告诉他: Don't You Ever Come Around Here “你胆敢再来? Don't Wanna See Your Face, 不想再见你, You Be ...
- php如何在原来的时间上加一天?一小时
php如何在原来的时间上加一天?一小时? <?phpecho "今天:",date('Y-m-d H:i:s'),"<br>";echo &q ...
- [置顶] MongoDB 分布式操作——分片操作
MongoDB 分布式操作——分片操作 描述: 像其它分布式数据库一样,MongoDB同样支持分布式操作,且MongoDB将分布式已经集成到数据库中,其分布式体系如下图所示: 所谓的片,其实就是一个单 ...
- python的str,unicode对象的encode和decode方法
python的str,unicode对象的encode和decode方法 python中的str对象其实就是"8-bit string" ,字节字符串,本质上类似java中的byt ...