更好的抽屉效果(ios)
昨天项目基本没啥事了,晚上早早的就回家了,躺在床上无聊地玩着手机(Android的),在清理系统垃圾时被一个“360手机助手”给吸引了,
其实我是被它的那个抽屉效果给吸引了,此时你也许会觉得我out了 ,一个抽屉效果有啥好吸引人的。
以前在项目中我也用到过抽屉,也看过大量的抽屉效果,大部分时间时只有一个view可以滑动的,下面那个view是不动的,就像是拉出或推出一个view的效果差不多,
但看到这个 360手机助手的抽屉效果时,我觉得原来的那些真没这个好看。在这个程序中,当你左右拖动那个view A时,另外一个view B也会相应的滑动,但滑动的幅度没有你拖动的那个view A大,不知道我表达清楚没有,你可以下载个360手机助手看看。
于是今天就模仿了一下,拖动view A时 两个view都可以滑动,则说明动画时作用两个view的。
下面直接上代码吧,代码很简单,也没具体完善逻辑,只是个简单的效果实现 ,了解抽屉效果的很容易就看懂的
1.工程结构图

2.主要的代码
//
// Drawer.h
// SlideDrawer
//
// Created by PSH_Chen_Tao on 10/12/13.
// Copyright (c) 2013 wolfman. All rights reserved.
// #import <UIKit/UIKit.h> //表明当前状态的枚举常量
typedef enum{
DrawerStatusLeft,
DrawerStatusRight
}DrawerStatus; @interface Drawer : UIView //初始化方法
-(id)initWithParent:(UIViewController *)parentViewController firstContent:(UIViewController *)firstContentViewController secondContent:(UIViewController *)secondContentViewController; @end
//
// Drawer.m
// SlideDrawer
//
// Created by PSH_Chen_Tao on 10/12/13.
// Copyright (c) 2013 wolfman. All rights reserved.
// #import "Drawer.h"
#define kDistance 50
//#define firstContentMoveDistance 100
@implementation Drawer{
UIViewController *parent; //控制第一个内容view的controller
UIViewController *firstContent;
//控制第二个内容view的controller
UIViewController *secondContent; // 左滑时 firstContent的 view的center
CGPoint firstLeft;
// 右滑时 firstContent的 view的center
CGPoint firstRight; // 左滑时 secondContent的 view的center
CGPoint secondLeft;
// 右滑时 secondContent的 view的center
CGPoint secondRight; // 目前的状态
DrawerStatus status; // firstContent 的 center
CGPoint firstContentCenter; //移动比列,这个是关键,本程序是类似360手机助手一样的效果,两边的view都是
//可同时移动的,在此就需要一个很好的匹配,移动时两个view的间距不能增大或减小
float moveScale;
} -(id)initWithParent:(UIViewController *)parentViewController firstContent:(UIViewController *)firstContentViewController secondContent:(UIViewController *)secondContentViewController{
parent = parentViewController;
firstContent = firstContentViewController;
secondContent = secondContentViewController;
// 为了便于效果查看
firstContent.view.backgroundColor = [UIColor redColor];
secondContent.view.backgroundColor = [UIColor greenColor]; //设置frame
self = [super initWithFrame:CGRectMake(, , parent.view.frame.size.width, parent.view.frame.size.height)];
if (self) {
firstContent.view.frame = CGRectMake(, , self.frame.size.width, parent.view.frame.size.height);
[self addSubview:firstContent.view];
secondContent.view.frame = CGRectMake(, , parent.view.frame.size.width, parent.view.frame.size.height);
[self addSubview:secondContent.view]; // 下面算firstContent 和 secondContent的左右中心点时是要遵循一定关系的,
//firstContent 和 secondContent 的可移动距离之和必须等于 parent的宽度
firstLeft = CGPointMake(self.frame.size.width/ - kDistance, self.frame.size.height/);
firstRight = self.center; secondLeft = self.center;
secondRight = CGPointMake(self.frame.size.width+secondContent.view.frame.size.width/-kDistance, self.frame.size.height/); firstContent.view.center = firstRight;
secondContent.view.center = secondRight;
status = DrawerStatusRight; // 加入手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
tap.numberOfTapsRequired = ;
tap.enabled = YES;
[secondContent.view addGestureRecognizer:tap]; UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
pan.enabled = YES;
[secondContent.view addGestureRecognizer:pan]; //设置第一个view 的起始center
firstContentCenter = firstContent.view.center; //这个是关键,为什么要这样算,为了防止两个content view之间的间距发生改变,
//所以firstContent 和 secondContent 的可移动距离之和必须等于 parent的宽度
moveScale = (float)kDistance/(float)(self.frame.size.width - kDistance);
} return self;
} -(void)handleTap:(UITapGestureRecognizer *)tap{
// 当secondController完全出现在屏幕中时,则只有点击左上角时才有用
// 呵呵,没啥好写的,就是简单地控制下点击范围
if (status == DrawerStatusLeft) { CGPoint p = [tap locationInView:secondContent.view];
if (p.x > kDistance || p.y > ) {
return;
}
} [UIView animateWithDuration:0.3 delay:0.01 options:UIViewAnimationOptionCurveLinear animations:^{
if (status == DrawerStatusRight) {
secondContent.view.center = secondLeft; firstContent.view.center = firstLeft;
status = DrawerStatusLeft;
}else{
secondContent.view.center = secondRight;
firstContent.view.center = firstRight;
status = DrawerStatusRight;
}
} completion:nil]; } -(void)handlePan:(UIPanGestureRecognizer *)pan{ CGPoint point = [pan translationInView:self]; if (secondContent.view.center.x + point.x < secondLeft.x) { secondContent.view.center = secondLeft;
firstContent.view.center = firstLeft; }else if (secondContent.view.center.x + point.x > secondRight.x){ secondContent.view.center = secondRight; firstContent.view.center = firstRight; }else{
secondContent.view.center = CGPointMake(secondContent.view.center.x + point.x, secondContent.view.center.y);
//firstContent的移动距离必须按照比例计算
firstContent.view.center = CGPointMake(firstContent.view.center.x + point.x*moveScale, firstContent.view.center.y);
} [pan setTranslation:CGPointMake(, ) inView:self];
if (pan.state == UIGestureRecognizerStateEnded) {
[UIView animateWithDuration:0.3 delay:0.01 options:UIViewAnimationOptionCurveLinear animations:^{
if (secondContent.view.center.x < secondRight.x*/) { secondContent.view.center = secondLeft; firstContent.view.center = firstLeft;
status = DrawerStatusLeft;
}else{ secondContent.view.center = secondRight;
firstContent.view.center = firstRight; status = DrawerStatusRight;
}
} completion:nil];
}
} @end
上面两段是主要的代码了,下面是使用的地方
//
// ViewController.m
// SlideDrawer
//
// Created by PSH_Chen_Tao on 10/12/13.
// Copyright (c) 2013 wolfman. All rights reserved.
// #import "ViewController.h" #import "FirstViewController.h"
#import "SecondViewController.h"
#import "Drawer.h"
@interface ViewController () @end @implementation ViewController - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. FirstViewController *fisrt = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil]; SecondViewController *second = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil]; Drawer *drawer = [[Drawer alloc]initWithParent:self firstContent:fisrt secondContent:second];
[self.view addSubview:drawer];
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
呵呵,可以看看效果,是不是感觉好点。。
更好的抽屉效果(ios)的更多相关文章
- iOS开发之抽屉效果实现
		
说道抽屉效果在iOS中比较有名的第三方类库就是PPRevealSideViewController.一说到第三方类库就自然而然的想到我们的CocoaPods,今天的博客中用CocoaPods引入PPR ...
 - ios开发中超简单抽屉效果(MMDrawerController)的实现
		
ios开发中,展示类应用通常要用到抽屉效果,由于项目需要,本人找到一个demo,缩减掉一些不常用的功能,整理出一个较短的实例. 首先需要给工程添加第三方类库 MMDrawerController: 这 ...
 - 玩转iOS开发 - 简易的实现2种抽屉效果
		
BeautyDrawer BeautyDrawer 是一款简单易用的抽屉效果实现框架,集成的属性能够对view 滑动缩放进行控制. Main features 三个视图,主视图能够左右滑动.实现抽屉效 ...
 - iOS中 超简单抽屉效果(MMDrawerController)的实现
		
ios开发中,展示类应用通常要用到抽屉效果,由于项目需要,本人找到一个demo,缩减掉一些不常用的功能,整理出一个较短的实例. 首先需要给工程添加第三方类库 MMDrawerController: 这 ...
 - iOS实现抽屉效果
		
抽屉效果 在iOS中非常多应用都用到了抽屉效果,比如腾讯的QQ,百度贴吧- --- 1. 终于效果例如以下图所看到的 --- 2.实现步骤 1.開始启动的时候.新建3个不同颜色的View的 1.设置3 ...
 - iOS开发——实用技术OC篇&简单抽屉效果的实现
		
简单抽屉效果的实现 就目前大部分App来说基本上都有关于抽屉效果的实现,比如QQ/微信等.所以,今天我们就来简单的实现一下.当然如果你想你的效果更好或者是封装成一个到哪里都能用的工具类,那就还需要下一 ...
 - iOS详解MMDrawerController抽屉效果(一)
		
提前说好,本文绝对不是教你如何使用MMDrawerController这个第三方库,因为那太多人写了 ,也太简单了.这篇文章主要带你分析MMDrawerController是怎么实现抽屉效果,明白 ...
 - iOS LeftMenu抽屉效果与ScrollView共存时的手势冲突
		
公司有个项目,需要做左侧滑动,首页是ScrollView嵌套TableView.首页是一个ScrollView,所以当contentOffset是0.0的时候,无法直接滑动出抽屉效果,用户体验感非常差 ...
 - iOS抽屉效果
		
源代码下载 抽屉效果第三方类库下载 所需第三方类库下载 側拉栏抽屉效果图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTUhUaW9z/font/5a6L ...
 
随机推荐
- Bag标签之中的一个行代码实行中文分词实例1
			
例1: 分词(返回以逗号隔开的词组,gap=",") <bagid=pPage act=2words name=words gap=",">我喜欢黄 ...
 - hdu 5053 the Sum of Cube(上海网络赛)
			
the Sum of Cube Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
 - Oracle利用存储过程性 实现分页
			
分页的简单配置 在上一次已经说过了 这边说说怎么在存储过程中实现分页 首先建立存储过程 參考 http://www.cnblogs.com/gisdream/archive/2011/11/16/22 ...
 - 经验总结35--IP地址区域匹配
			
想知道客服端訪问的IP地址是多少,并知道区域. 一般能够去http://www.ip138.com/,输入IP查询,但没提供比較好的接口,程序使用不方便. 另外有些企业提供一些离线的IP数据库,能够进 ...
 - APP 半自适应 WEB页面
			
特别赶,响应式纯自适应的,有空写了新的发. (在手机上看,页面上看一定乱) <!DOCTYPE html><html> <head> <meta http-e ...
 - svg的自述
			
svg可缩放矢量图形(Scalable Vector Graphics). SVG 使用 XML 格式定义图像. SVG 是使用 XML 来描述二维图形和绘图程序的语言. 什么是SVG? SVG 指可 ...
 - php中echo(),print(),print_r()用法
			
原文 php中echo(),print(),print_r()用法 从我对echo(),print(),print_r()这个函数的理解是echo可输入字符串变量常量,print与echo差不多,但p ...
 - 【hoj】2651 pie 二分查找
			
二分查找是一个非常主要的算法,针对的是有序的数列,通过中间值的大小来推断接下来查找的是左半段还是右半段,直到中间值的大小等于要找到的数时或者中间值满足一定的条件就返回,所以当有些问题要求在一定范围内找 ...
 - 【UVA】10285-Longest Run on a Snowboard(动态规划)
			
这是一个简单的问题.你并不需要打印路径. 状态方程dp[i][j] = max(dp[i-1][j],dp[i][j-1],dp[i+1][j],dp[i][j+1]); 14003395 10285 ...
 - 组件接口(API)设计指南[5]-最后的思考
			
*阅读其它章节: http://blog.csdn.net/cuibo1123/article/details/39894477 最后的思考 我通过困难的学习以及多年的失误.写了这片篇关于创建组件和a ...