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 抽屉效果的导航菜单实现 抽屉效果的导航菜单 看了很多应用,觉得这种侧滑的抽屉效果的菜单很好. 不用切换到另一个页面,也不用去按菜单的硬件按钮,直接在界面上一个按钮点击,菜单就滑出来,而 ...
随机推荐
- 去除android手机滚动条
方法1:::-webkit-scrollbar{display: none;} 方法2:::-webkit-scrollbar{height:0; width:0:}
- CodeForces 710F 强制在线AC自动机
题目链接:http://codeforces.com/contest/710/problem/F 题意:维护一个集合,集合要求满足三种操作. 1 str:向集合插入字符串str(保证不会插入之前已经插 ...
- jade学习02
模版继承 ; block,extends ;如果是原生html文件的话,后缀html //layout.jade doctype html html head meat(charset='utf-8' ...
- UE4在C++中使用OnComponentBeginOverlap之类的时间
首先说明一下,官方文档是错的,在4.10版本下,绑定函数在角色类的构造函数中不起作用.2016.2.12 这里角色类为例 首先在头文件中添加: UFUNCTION() void OnOverlapBe ...
- java-类
浏览以下内容前,请点击并阅读 声明 java是面向对象的语言,而对象的创建,则需要借助类,类可以说是一个创建对象的模具(个人理解). 类的定义 以下构成定义类的最简单(不能再简单)语句: class ...
- ACM 国王的魔镜
国王的魔镜 时间限制:3000 ms | 内存限制:65535 KB 难度:1 描述 国王有一个魔镜,可以把任何接触镜面的东西变成原来的两倍——只是,因为是镜子嘛,增加的那部分是反的. 比如一 ...
- jquery validate学习心得
据说,javascript最初的由来就是为了解决表单的验证问题,当然现在的js已经能够用来做各种各样炫酷的效果,不过表单验证一直以来都是js很重要的一个用途(在博客园第一篇博客,不知道说些什么开头~~ ...
- [深入浅出WP8.1(Runtime)]应用实例——移动截图
10.2应用实例——移动截图 移动截图例子是实现一个把一张图片的某个部分截取出来的功能,并且用户可以选定截取的图片区间.那个该例子会使用ManipulationDelta事件来实现对截取区间的选择.然 ...
- 闲扯淡json格式与对象
在这里推荐使用http://json.tongxiehui.net/ 这个在线解析jso格式 JSON 语法是 JavaScript 对象表示语法的子集. 数据在名称/值对中 数据由逗号分隔 花括号保 ...
- jquery.autocomplete在火狐下的BUG解决
该插件在火狐下使用拼音输入法输入,回车后无法即使响应事件,需要再输入别的字母才会响应事件 主要原因: 该插件响应主要监听了keydown事件 其他浏览器在打开中文输入法的情况下输入的拼 ...