昨天项目基本没啥事了,晚上早早的就回家了,躺在床上无聊地玩着手机(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. Tomcat剖析(三):连接器(2)

    Tomcat剖析(三):连接器(2) 1. Tomcat剖析(一):一个简单的Web服务器 2. Tomcat剖析(二):一个简单的Servlet服务器 3. Tomcat剖析(三):连接器(1) 4 ...

  2. Oracle 11g 环境,使用utl_smtp创建一个存储过程来发送邮件

    太多的在线电子邮件存储过程.我不转发,弄个作为一个简单的例子演示. create or replace procedure Send_mail(mail_body varchar2) is smtp_ ...

  3. .NET平台机器学习

    .NET平台机器学习资源汇总,有你想要的么? 接触机器学习1年多了,由于只会用C#堆代码,所以只关注.NET平台的资源,一边积累,一边收集,一边学习,所以在本站第101篇博客到来之际,分享给大家.部分 ...

  4. 【百度地图API】如何制作孪生姐妹地图?

    原文:[百度地图API]如何制作孪生姐妹地图? 任务描述: 我想要两张一模一样的地图!我想要双子地图!我想要孪生姐妹地图! 好好好,统统满足大家! 在这里我不需要使用百度地图API提供的地图缩略图控件 ...

  5. The Swift Programming Language-官方教程精译Swift(6)控制流--Control Flow

    Swift提供了类似C语言的流程控制结构,包括可以多次执行任务的for和while循环,基于特定条件选择执行不同代码分支的if和switch语句,还有控制流程跳转到其他代码的break和continu ...

  6. 多线程学习之五超时模式Timer

    timed[超时模式]案例:一个线程提供下载数据,另一个线程执行下载,如果有5秒钟以上,提供下载的线程没有提供数据,下载线程因超时异常,停止下载线程运行. 超时异常类 /** * */ package ...

  7. windows下用c实现Socket通信

    原文:windows下用c实现Socket通信 原本以为c是跨平台,所以,c在windows下和linux下的程序应该是类似于Java,什么都不用改变的,今儿才恍然大悟,他们的类库不一样啊-- 下面我 ...

  8. 图解IntelliJ IDEA v13应用服务器的运行配置

    初步了解IntelliJ IDEA v13应用服务器以后,接下来我们将继续设置应用服务器的运行配置. Artifacts是IDE在通过运行配置时部署的一个服务.Artifacts包括名称.类型.输出目 ...

  9. C# 编译器选项 /platform(指定输出平台)32位程序运行到x64平台的问题

    如果说你编译的exe运行时报错: “尝试读取或写入受保护的内存.这通常指示其他内存已损坏” 这很有可能是你是以非托管的方式错误地引用了64位的API中去. 为什么会这样? 那你就要考虑VS的编译器选项 ...

  10. openwrt驱动与应用程序的联系

    应用程序与驱动之间需要进行命令的传递,因而它们之间需要共同定义一套双方都可以识别的数据结构,实际使用时它们include的是名字和内容相同但位置不同的头文件. 比如spi_gpio_ad7193.h这 ...