昨天项目基本没啥事了,晚上早早的就回家了,躺在床上无聊地玩着手机(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)的更多相关文章

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

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

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

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

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

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

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

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

  5. iOS实现抽屉效果

    抽屉效果 在iOS中非常多应用都用到了抽屉效果,比如腾讯的QQ,百度贴吧- --- 1. 终于效果例如以下图所看到的 --- 2.实现步骤 1.開始启动的时候.新建3个不同颜色的View的 1.设置3 ...

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

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

  7. iOS详解MMDrawerController抽屉效果(一)

      提前说好,本文绝对不是教你如何使用MMDrawerController这个第三方库,因为那太多人写了 ,也太简单了.这篇文章主要带你分析MMDrawerController是怎么实现抽屉效果,明白 ...

  8. iOS LeftMenu抽屉效果与ScrollView共存时的手势冲突

    公司有个项目,需要做左侧滑动,首页是ScrollView嵌套TableView.首页是一个ScrollView,所以当contentOffset是0.0的时候,无法直接滑动出抽屉效果,用户体验感非常差 ...

  9. iOS抽屉效果

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

随机推荐

  1. 两种计算和输出n内5要么9除尽互惠

    #include<stdio.h> int main() { int i=0,n=0; float fSum=0; scanf("%d", &n); for ( ...

  2. css3布局相关(持续更新)

    1三栏布局,两边定宽,中间自适应 2让文字位于div元素的正中央 3不管浏览器窗口如何变化,让一张图片始终显示在浏览器正中央.

  3. CSharp设计模式读书笔记(18):中介者模式(学习难度:★★★☆☆,使用频率:★★☆☆☆)

    中介者模式(Mediator Pattern):用一个中介对象(中介者)来封装一系列的对象交互,中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互,中介者模式又称为 ...

  4. easyui datagrid footer 页脚问题

    mvc 的一个例子 public string IndexV2() { var dataGridJson = new DataGridJson(); var data = new List<My ...

  5. 直读Innodb datafile

    这两天有空翻了翻大神写的<innodb存储引擎>,手痒亲身实践.由于此书出版了有段时日,没有用其推荐的python工具,通过点滴推敲,略微发现其中冰山一角的奥秘.对于今后对于一些问题查证或 ...

  6. [Openstack] Expecting an auth URL via either --os-auth-url or env[OS_AUTH_URL]

    直接使用devstack在ubuntu14.04单个节点的建筑openstack 使用keystone查询租户和用户始终报告时,这个错误! 主要看下这些配置是否正确.我们将能够解决这个问题 opens ...

  7. poj 3273 Monthly Expense (二分)

    //最大值最小 //天数的a[i]值是固定的 不能改变顺序 # include <algorithm> # include <string.h> # include <s ...

  8. Pki原则

    核心提示: 公开密钥和公开密钥证明书,产生的私钥client要么server证书.加密的公共密钥才能解密私钥文件只.私钥只能解密公开的加密文件.公众认为,它是开放的.所有的人都能够得到它.私人还表明, ...

  9. MonkeyDevcie API 实践全记录

    1.    背景 使用SDK自带的NotePad应用作为实践目标应用,目的是对MonkeyDevice拥有的成员方法做一个初步的了解. 以下是官方列出的方法的Overview. Return Type ...

  10. 原生js写的一个弧形菜单插件

    弧形菜单是一种半弧式或者全弧形菜单,是一种不同于传统横向或者竖向菜单形式的菜单.最近在网上看到好多人写出了这种效果,于是也尝试自己写了一个. 实现方式:原生态js 主要结构: 1.参数合并 var d ...