IOS第15天(2,事件处理,侧滑菜单,抽屉效果)
******HMDrawViewController.m
#import "HMDrawViewController.h" @interface HMDrawViewController () @property (nonatomic, assign) BOOL isDraging;
@end @implementation HMDrawViewController - (void)viewDidLoad
{
// UIViewController
[super viewDidLoad];
// Do any additional setup after loading the view. // 1.添加子控件
[self addChildView];
#warning 第三步 观察_mainView的frame改变
// 2.监听
/**
* 给_mainView添加一个观察者
*
* KeyPath:监听frame这个属性
*
* options:监听新值的改变
*/
[_mainView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil]; } // 当_mainView的frame属性改变的时候就会调用
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"%@", NSStringFromCGRect(_mainView.frame)); if (_mainView.frame.origin.x < ) { // 往左移动
// 显示右边
_rightView.hidden = NO;
// 隐藏左边
_leftView.hidden = YES;
}else if (_mainView.frame.origin.x > ){ // 往右移动
// 显示左边
_rightView.hidden = YES;
// 隐藏右边
_leftView.hidden = NO; }
} #warning 第一步
- (void)addChildView
{
// left
UIView *leftView = [[UIView alloc] initWithFrame:self.view.bounds];
leftView.backgroundColor = [UIColor greenColor];
[self.view addSubview:leftView];
_leftView = leftView; // right
UIView *rightView = [[UIView alloc] initWithFrame:self.view.bounds];
rightView.backgroundColor = [UIColor blueColor];
[self.view addSubview:rightView];
_rightView = rightView; // mainView
UIView *mainView = [[UIView alloc] initWithFrame:self.view.bounds];
mainView.backgroundColor = [UIColor redColor];
[self.view addSubview:mainView];
_mainView = mainView;
} #warning 第二布
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// 获取UITouch对象
UITouch *touch = [touches anyObject]; // 获取当前点
CGPoint currentPoint = [touch locationInView:self.view]; // 获取上一个点
CGPoint prePoint = [touch previousLocationInView:self.view]; // x轴偏移量:当手指移动一点的时候,x偏移多少
CGFloat offsetX = currentPoint.x - prePoint.x; // 设置当前主视图的frame
_mainView.frame = [self getCurrentFrameWithOffsetX:offsetX]; _isDraging = YES;
}
#warning 第四步
#define HMMaxY 60
// 当手指偏移一点,根据X轴的偏移量算出当前主视图的frame
- (CGRect)getCurrentFrameWithOffsetX:(CGFloat)offsetX
{
CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
CGFloat screenH = [UIScreen mainScreen].bounds.size.height; // 获取y轴偏移量,手指每移动一点,y轴偏移多少
CGFloat offsetY = offsetX * HMMaxY / screenW; CGFloat scale = (screenH - * offsetY) / screenH; if (_mainView.frame.origin.x < ) { // 往左边滑动
scale = (screenH + * offsetY) / screenH;
} // 获取之前的frame
CGRect frame = _mainView.frame;
frame.origin.x += offsetX;
frame.size.height = frame.size.height *scale;
frame.size.width = frame.size.width *scale;
frame.origin.y = (screenH - frame.size.height) * 0.5; return frame;
} #define HMRTarget 250
#define HMLTarget -220
/*
_mainView.frame.origin.x > screenW * 0.5 定位到右边
CGRectGetMaxX(_mainView.frame) < screenW * 0.5 定位到左边 -220 */
// 定位
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{ // 复位
if (_isDraging == NO && _mainView.frame.origin.x != ) {
[UIView animateWithDuration:0.25 animations:^{ _mainView.frame = self.view.bounds;
}];
} CGFloat screenW = [UIScreen mainScreen].bounds.size.width; CGFloat target = ;
if (_mainView.frame.origin.x > screenW * 0.5) { // 定位到右边
target = HMRTarget;
}else if (CGRectGetMaxX(_mainView.frame) < screenW * 0.5) { // 定位到左边
target = HMLTarget;
} [UIView animateWithDuration:0.25 animations:^{ if (target) { // 在需要定位左边或者右边 // 获取x轴偏移量
CGFloat offsetX = target - _mainView.frame.origin.x; // 设置当前主视图的frame
_mainView.frame = [self getCurrentFrameWithOffsetX:offsetX]; }else{ // 还原
_mainView.frame = self.view.bounds;
}
}]; _isDraging = NO; } @end
***HMDrawViewController.h文件
#import <UIKit/UIKit.h> @interface HMDrawViewController : UIViewController @property (nonatomic, weak, readonly) UIView *mainView;
@property (nonatomic, weak, readonly) UIView *leftView;
@property (nonatomic, weak, readonly) UIView *rightView; @end
IOS第15天(2,事件处理,侧滑菜单,抽屉效果)的更多相关文章
- iOS开发资源:几个类似Path 2.0侧滑菜单的效果实现
IIViewDeckController/ViewDeck 类似 Path 2.0 的视图左右滑动的效果,可向左或者向右顺滑的滑动.支持ARC和non-ARC,默认ARC. https://githu ...
- IOS第15天(3,事件处理,手势处理)
7> 手势识别 使用UIImageView原因:之前既能看见图片,又能监听点击的只有UIButton,学了手势,我们的UIImageView也可以. * tap(代理:左边不能点,右 ...
- IOS第15天(2,事件处理hitTest练习)
***hitTest 获取最合适的点 @implementation HMGreenView - (void)touchesBegan:(NSSet *)touches withEvent:(UIEv ...
- IOS第15天(1,事件处理View的拖拽)
*******view 一些方法 #import "HMView.h" @implementation HMView // 一个完整的触摸过程 // touchesBegan -& ...
- Android SlidingMenu侧滑菜单使用
把下载的侧滑菜单压缩包打开,会有一个library文件夹,在eclipse中import existing android code into workspace,导入library文件夹,并且选择作 ...
- Android 自定义View修炼-仿QQ5.0 的侧滑菜单效果的实现
有一段时间没有写博客了,最近比较忙,没什么时间写,刚好今天有点时间, 我就分享下,侧滑菜单的实现原理,一般android侧滑的实现原理和步骤如下:(源码下载在下面最后给出哈) 1.使用ViewGrou ...
- DrawerLayoutDemo【侧边栏(侧滑菜单)简单实现】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 简单实现侧边栏(侧滑菜单)效果: 点击触发打开左侧侧边栏,手势滑动关闭左侧侧边栏: 手势滑动打开右侧侧边栏,手势滑动关闭右侧侧边栏: ...
- 自定义控件?试试300行代码实现QQ侧滑菜单
Android自定义控件并没有什么捷径可走,需要不断得模仿练习才能出师.这其中进行模仿练习的demo的选择是至关重要的,最优选择莫过于官方的控件了,但是官方控件动辄就是几千行代码往往可能容易让人望而却 ...
- Android 抽屉效果的导航菜单实现
Android 抽屉效果的导航菜单实现 抽屉效果的导航菜单 看了很多应用,觉得这种侧滑的抽屉效果的菜单很好. 不用切换到另一个页面,也不用去按菜单的硬件按钮,直接在界面上一个按钮点击,菜单就滑出来,而 ...
随机推荐
- loopback 02
数据库连接操作,以mongodb为例 安装loopback-connector-mongodb 修改datasources.json //例子 { "db": { "na ...
- express-9 Handlebars模板引擎(2)
视图和布局 视图通常表现为网站上的各个页面(它也可以表现为页面中AJAX局部加载的内容,或一封电子邮件,或页面上的任何东西).默认情况下,Express会在views子目录中查找视图.布局是一种特殊的 ...
- AutoMapper简明教程(学习笔记)
].FirstName + nameDto12[].LastName);Console.WriteLine();//Console.ReadKey();//emitMapper error//List ...
- 使用Spring发送邮件
http://www.oschina.net/code/snippet_253813_36503
- enum枚举类型的使用
修饰符为public static enum,不用加final,否则提示错误. 枚举类的所有实例必须在枚举类中显式列出(,分隔,; 结尾).列出的实例系统会自动添加 public static fin ...
- React版本修改内容
React新版本(0.12.2)在2014年12月18日发布,对比了我之前用的v0.11.2版本改动很大,基本的写法被扩展,让我顿时感觉自己又要重新开始.坑啊~ 事已至此,必须重新适应,首先我们来看看 ...
- tidyr包--数据处理包
tidyr包的作者是Hadley Wickham.这个包常跟dplyr结合使用.本文将介绍tidyr包中下述四个函数的用法: gather—宽数据转为长数据.类似于reshape2包中的melt函数 ...
- BZOJ3740 : pku2842 N-dimension Matching N维匹配
做$n$次递推求出$n$维Hash值即可,时间复杂度$O(nk)$. #include<cstdio> #define N 500010 int n,i,j,t,delta,ca,cb,a ...
- samba 挂载 问题
link: http://www.minunix.com/2013/04/linux-mount-samba/ http://my.oschina.net/laopiao/blog/161648 最近 ...
- [转]CentOS下安装伪分布式Hadoop-1.2.1
From: http://blog.csdn.net/yinan9/article/details/16805275 环境:CentOS 5.10(虚拟机下) [root@localhost hado ...