******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,事件处理,侧滑菜单,抽屉效果)的更多相关文章

  1. iOS开发资源:几个类似Path 2.0侧滑菜单的效果实现

    IIViewDeckController/ViewDeck 类似 Path 2.0 的视图左右滑动的效果,可向左或者向右顺滑的滑动.支持ARC和non-ARC,默认ARC. https://githu ...

  2. IOS第15天(3,事件处理,手势处理)

    7> 手势识别    使用UIImageView原因:之前既能看见图片,又能监听点击的只有UIButton,学了手势,我们的UIImageView也可以.    * tap(代理:左边不能点,右 ...

  3. IOS第15天(2,事件处理hitTest练习)

    ***hitTest 获取最合适的点 @implementation HMGreenView - (void)touchesBegan:(NSSet *)touches withEvent:(UIEv ...

  4. IOS第15天(1,事件处理View的拖拽)

    *******view 一些方法 #import "HMView.h" @implementation HMView // 一个完整的触摸过程 // touchesBegan -& ...

  5. Android SlidingMenu侧滑菜单使用

    把下载的侧滑菜单压缩包打开,会有一个library文件夹,在eclipse中import existing android code into workspace,导入library文件夹,并且选择作 ...

  6. Android 自定义View修炼-仿QQ5.0 的侧滑菜单效果的实现

    有一段时间没有写博客了,最近比较忙,没什么时间写,刚好今天有点时间, 我就分享下,侧滑菜单的实现原理,一般android侧滑的实现原理和步骤如下:(源码下载在下面最后给出哈) 1.使用ViewGrou ...

  7. DrawerLayoutDemo【侧边栏(侧滑菜单)简单实现】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 简单实现侧边栏(侧滑菜单)效果: 点击触发打开左侧侧边栏,手势滑动关闭左侧侧边栏: 手势滑动打开右侧侧边栏,手势滑动关闭右侧侧边栏: ...

  8. 自定义控件?试试300行代码实现QQ侧滑菜单

    Android自定义控件并没有什么捷径可走,需要不断得模仿练习才能出师.这其中进行模仿练习的demo的选择是至关重要的,最优选择莫过于官方的控件了,但是官方控件动辄就是几千行代码往往可能容易让人望而却 ...

  9. Android 抽屉效果的导航菜单实现

    Android 抽屉效果的导航菜单实现 抽屉效果的导航菜单 看了很多应用,觉得这种侧滑的抽屉效果的菜单很好. 不用切换到另一个页面,也不用去按菜单的硬件按钮,直接在界面上一个按钮点击,菜单就滑出来,而 ...

随机推荐

  1. git merge 与 rebase 的区别

    http://gitbook.liuhui998.com/4_2.html merge rebase

  2. 关于Ue4的深度模板

    http://www.unrealchina.net/forum.php?mod=viewthread&tid=100234

  3. HDU5008 Boring String Problem(后缀数组 + 二分 + 线段树)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5008 Description In this problem, you are given ...

  4. javaScript怪癖分析

    最近了解到javascript中有些编程怪癖现象,很有意思,有必要总结一下: 1.未知变量名创建全局变量 在我们平常的编写javascript程序的时候,有的人写法不是很正规,在定义变量的时候 直接定 ...

  5. BZOJ2040 : [2009国家集训队]拯救Protoss的故乡

    以根为原点,所有叶子为汇点建立网络. 对于一条边$(x,y,A,B)$,$x$向$y$连边,容量$A$,费用0,再连边,容量$B-A$,费用1. 然后不断增广,直到费用达到$M$为止的最大流即为答案. ...

  6. 如何查看项目svn路径

    1.选择项目根目录---->鼠标右键---->属性---->版本控制(Subversion) 如图:

  7. 状压dp题目总结

    这块比较薄弱.. 来几道水题: BZOJ1231: [Usaco2008 Nov]mixup2 混乱的奶牛 f[i][j]状态i结尾j的个数 ;i<=tot;i++) ;j<=n;j++) ...

  8. Linux安装卸载查看vsftpd

    Linux & vsftpd 相关的命令: 查看---rpm -qa | grep vsftpd 卸载---rpm -e vsftpd 安装---rpm  -ivh /media/(在此tab ...

  9. Delphi中对BCD码的直接支持 (转)

    最近在Delphi下写软件,需要将数据转换为BCD码和将BCD码转换为其它数据类型,从网上搜索了一下,没有发现好的函数,于是就想自定义函数来完成BCD与其它格式的数据转换功能.但最终没有动手写,先查查 ...

  10. 20145330《Java学习笔记》第一章课后练习8知识总结以及IDEA初次尝试

    20145330<Java学习笔记>第一章课后练习8知识总结以及IDEA初次尝试 题目: 如果C:\workspace\Hello\src中有Main.java如下: package cc ...