#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抽屉效果的更多相关文章

  1. iOS开发——高级篇——iOS抽屉效果实现原理

    实现一个简单的抽屉效果: 核心思想:KVO实现监听mainV的frame值的变化 核心代码: #import "ViewController.h" // @"frame& ...

  2. iOS抽屉效果

    源代码下载 抽屉效果第三方类库下载 所需第三方类库下载 側拉栏抽屉效果图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTUhUaW9z/font/5a6L ...

  3. AJ学IOS(26)UI之iOS抽屉效果小Demo

    AJ分享,必须精品 先看效果 实现过程 第一步,把三个view设置好,还有颜色 #warning 第一步 - (void)addChildView { // left UIView *leftView ...

  4. ios开发中超简单抽屉效果(MMDrawerController)的实现

    ios开发中,展示类应用通常要用到抽屉效果,由于项目需要,本人找到一个demo,缩减掉一些不常用的功能,整理出一个较短的实例. 首先需要给工程添加第三方类库 MMDrawerController: 这 ...

  5. iOS开发——实用技术OC篇&简单抽屉效果的实现

    简单抽屉效果的实现 就目前大部分App来说基本上都有关于抽屉效果的实现,比如QQ/微信等.所以,今天我们就来简单的实现一下.当然如果你想你的效果更好或者是封装成一个到哪里都能用的工具类,那就还需要下一 ...

  6. iOS中 超简单抽屉效果(MMDrawerController)的实现

    ios开发中,展示类应用通常要用到抽屉效果,由于项目需要,本人找到一个demo,缩减掉一些不常用的功能,整理出一个较短的实例. 首先需要给工程添加第三方类库 MMDrawerController: 这 ...

  7. 玩转iOS开发 - 简易的实现2种抽屉效果

    BeautyDrawer BeautyDrawer 是一款简单易用的抽屉效果实现框架,集成的属性能够对view 滑动缩放进行控制. Main features 三个视图,主视图能够左右滑动.实现抽屉效 ...

  8. iOS开发之抽屉效果实现

    说道抽屉效果在iOS中比较有名的第三方类库就是PPRevealSideViewController.一说到第三方类库就自然而然的想到我们的CocoaPods,今天的博客中用CocoaPods引入PPR ...

  9. 更好的抽屉效果(ios)

    昨天项目基本没啥事了,晚上早早的就回家了,躺在床上无聊地玩着手机(Android的),在清理系统垃圾时被一个“360手机助手”给吸引了, 其实我是被它的那个抽屉效果给吸引了,此时你也许会觉得我out了 ...

随机推荐

  1. C++中delete 和delete[]的区别

    c++告诉我们在回收new分配的单个对象的内存空间的时候用delete,   回收new[ ]分配的一组对象的内存空间的时候用 delete[ ]; #include <iostream> ...

  2. javascript数字验证输入

    javascript数字验证功能: <html> <body> <p>请输入数字.如果输入值不是数字,浏览器会弹出提示框.</p> <input ...

  3. SQL Server索引进阶:第五级,包含列

    原文地址: Stairway to SQL Server Indexes: Level 5, Included Columns 本文是SQL Server索引进阶系列(Stairway to SQL ...

  4. Web前端开发工程师为什么讨厌IE6!

  5. MVC过滤器基本使用

        Action过滤器 /// <summary> /// 执行代码前执行 /// </summary> /// <param name="filterCo ...

  6. iOS 切割图片

    - (UIImage *)CutImageWithImage:(UIImage *)image withRect:(CGRect)rect { //使用CGImageCreateWithImageIn ...

  7. C# 微信公众平台开发(4)-- 模版消息

    微信公众平台开发 --发送模版消息 发送模版消息是微信服务号给某个用户发送模版消息,类似于APP的推送通知: 1.添加模版消息 在页面的左上 有一个添加功能插件的 按钮,如题 添加完成后,我们就可以在 ...

  8. 删除Lb重复的数,用La输出(顺序表)

    #include<stdio.h> typedef int A; const int LIST_INIT_SIZE=100; const int LISTINCRMENT=10; type ...

  9. mysql server5.6.28 修改数据目录

    1.查看配置文件 mysql --help | grep my.cnf 列出使用哪个配置文件(顺序推) 2.service mysql stop 3.创建新目录 mkdir /data 4.迁移之前的 ...

  10. app微信支付服务器端php demo

    class Wxpay { /* 配置参数 */ private $config = array( 'appid' => "wxc92b12277f277355", /*微信 ...