UI基础:事件.响应链 分类: iOS学习-UI 2015-07-03 19:51 1人阅读 评论(0) 收藏
UIEvent:事件,是由硬件捕捉的一个代表用户操作操作设备的对象.
事件分三类:触摸事件.晃动事件.远程控制事件.
触摸事件:用户通过触摸设备屏幕操作对象,.输入数据.支持多点触摸,包含1个到多个触摸点.
UIView支持触摸事件(继承了UIResponder),而且支持多点触摸
使用时,需要定义UIView子类,重写触摸相关的方法.
1.刚开始触摸到屏幕的时候触发
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
}
2.触摸事件被意外中断的时候触发(如:来电)
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
}
3.当手指离开屏幕的时候触发
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
}
4.当手指在视图上移动的时候触发
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
}
UITouch 点击类
touchs 存放手指对象
如:
1.当单击时
UITouch *touch=[touches anyObject];
//获取手指对象的数组
2.但双击时
NSArray *allTouch=[touches allObjects];
//获取手指对象
UITouch *touch1=[allTouch firstObject];
UITouch *touch2=[allTouch lastObject];
手势:有规律的触摸
UITouch代表触摸在屏幕上的一根手指,可以获取触摸时间和触摸位置.
获取touch对象:touches集合中包含了视图上的所有手势.
响应者链
有多个响应者对象组成的链.
UIResponder 响应者类
iOS中所有能响应事件(触摸.晃动.远程事件)的对象都是响应者.
系统定义了一个抽象的父类UIResponder来表示响应者.其子类都是响应者.
检测触碰视图
硬件检测到触摸操作,会将信息交给UIApplication,开始检测.
检测过程:
UIApplication -> window ->viewController ->view ->检测所有子视图
处理触摸事件:
事件处理的顺序与触摸检测查询相反.(自己的用户交互关闭,就交给父类处理)
触摸的子视图 -> view -> viewController ->window ->UIApplication
阻断响应者链
响应者链可以被打断.无法完成检测查询过程.
视图类的属性:userInteractionEnabled 关闭后可以阻断查询过程
__其中,下面是在事件里面几个简单程序的主要代码段
1.实现点击屏幕一次换颜色.点击两次换父视图的颜色
//当手指离开屏幕的时候触发
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"%s",__FUNCTION__);
// UITouch 点击类
// touchs 存放手指对象
UITouch *touch=[touches anyObject];
if (1==touch.tapCount) {
// 当视图识别为单击,延迟执行之下的方法,看是否会有再次点击
[self performSelector:@selector(changeMyselfBackGroundColor) withObject:nil afterDelay:0.3];
// self.backgroundColor=[UIColor randomColor];
}else if(2==touch.tapCount){
// 当识别为双击的时候,取消之前的操作
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(changeMyselfBackGroundColor) object:nil];
self.superview.backgroundColor=[UIColor randomColor];
}
}
-(void)changeMyselfBackGroundColor{
self.backgroundColor=[UIColor randomColor];
}
其中,对UIcolor进行了类目,使其有个随机颜色的功能
@implementation UIColor (Random)
+(UIColor *)randomColor{
return [UIColor colorWithRed:COLORVALUE green:COLORVALUE blue:COLORVALUE alpha:1.0];
}
@end
2.实现缩放视图的功能
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
if (1==touches.count) {
return;
}else{
// 获取手指对象的数组
NSArray *allTouch=[touches allObjects];
//获取手指对象
UITouch *touch1=[allTouch firstObject];
UITouch *touch2=[allTouch lastObject];
// 获取两个手指的当前位置
CGPoint currenFirstPoint=[touch1 locationInView:self];
CGPoint currenSecondPoint=[touch2 locationInView:self];
// 获得两个手指当前的距离
CGFloat currentDistance=[self distanFromPoint:currenFirstPoint toPoint:currenSecondPoint];
// 获取之前两手指的位置
CGPoint previousFirstPoint=[touch1 previousLocationInView:self];
CGPoint previousSecondPoint=[touch2 previousLocationInView:self];
// 获取之前两手指之间的距离
CGFloat currentsDistance=[self distanFromPoint:previousFirstPoint toPoint:previousSecondPoint];
// 获取比例
CGFloat rate=currentDistance/currentsDistance;
// 缩放视图,中心点不变,修改bounds即可
self.bounds=CGRectMake(0, 0, self.frame.size.width*rate, self.frame.size.height*rate);
}
}
//封装计算两点之间的距离的方法
-(CGFloat)distanFromPoint:(CGPoint)fromPoint toPoint:(CGPoint)toPoint{
CGFloat dx=fromPoint.x-toPoint.x;
CGFloat dy=fromPoint.y-toPoint.y;
return sqrt(dx*dx+dy*dy);
}
3.实现视图随着手指移动
@implementation MoveView
//触摸的方法没必要完全实现
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
//先获取手指对象
UITouch *touch=[touches anyObject];
// 获取自身视图手指当前的手指位置
CGPoint currenPoint=[touch locationInView:self];
// 获取手指之前的位置
CGPoint perviousPoint=[touch previousLocationInView:self];
// 计算移动的增量
CGFloat dx=currenPoint.x-perviousPoint.x;
CGFloat dy=currenPoint.y-perviousPoint.y;
CGPoint center=self.center;
self.center=CGPointMake(center.x+dx, center.y+dy);
}
@end
版权声明:本文为博主原创文章,未经博主允许不得转载。
UI基础:事件.响应链 分类: iOS学习-UI 2015-07-03 19:51 1人阅读 评论(0) 收藏的更多相关文章
- linux常用的压缩与解压缩命令 分类: 学习笔记 linux ubuntu 2015-07-05 19:38 38人阅读 评论(0) 收藏
1.gzip 压缩 gzip 是压缩文件,压缩之后文件后缀为.gz 用法:gzip 选项 [文件] 2.gunzip 解压 这个命令与gzip的功能刚好相反,这个是解压. 用法 gunzip 选项 [ ...
- shell入门之函数应用 分类: 学习笔记 linux ubuntu 2015-07-10 21:48 77人阅读 评论(0) 收藏
最近在学习shell编程,文中若有错误的地方还望各位批评指正. 先来看一个简单的求和函数 #!/bin/bash #a test about function f_sum 7 8 function f ...
- Shell脚本编程入门(一) 分类: 学习笔记 linux ubuntu 2015-07-09 21:06 29人阅读 评论(0) 收藏
最近在学shell,记录一下. if语句的使用: 1.判断两个参数大小 #!/bin/sh #a test about if statement a=10 b=20 if [ $a -eq $b ]; ...
- linux中的网络通信指令 分类: 学习笔记 linux ubuntu 2015-07-06 16:02 134人阅读 评论(0) 收藏
1.write write命令通信是一对一的通信,即两个人之间的通信,如上图. 效果图 用法:write <用户名> 2.wall wall指令可将信息发送给每位同意接收公众信息的终端机用 ...
- ubuntu14.04使用root用户登录桌面 分类: 学习笔记 linux ubuntu 2015-07-05 10:30 199人阅读 评论(0) 收藏
ubuntu安装好之后,默认是不能用root用户登录桌面的,只能使用普通用户或者访客登录.怎样开启root用户登录桌面呢? 先用普通用户登录,然后切换到root用户,然后执行如下命令: vi /usr ...
- linux中echo的用法 分类: 学习笔记 linux ubuntu 2015-07-14 14:27 21人阅读 评论(0) 收藏
1.echo命令我们常用的选项有两个,一个是-n,表示输出之后不换行,另外一个是-e,表示对于转义字符按相应的方式处理,如果不加-e那么对于转义字符会按普通字符处理. 2.echo输出时的转义字符 \ ...
- shell脚本调试 分类: 学习笔记 linux ubuntu 2015-07-14 12:49 53人阅读 评论(0) 收藏
1.sh -x script 这将执行脚本并显示所有变量的值 如,脚本: #!/bin/bash #a test about shift if [ $# -le 0 ] then echo " ...
- shell入门之流程控制语句 分类: 学习笔记 linux ubuntu 2015-07-10 16:38 89人阅读 评论(0) 收藏
1.case 脚本: #!/bin/bash #a test about case case $1 in "lenve") echo "input lenve" ...
- shell入门之变量测试 分类: 学习笔记 linux ubuntu 2015-07-10 15:49 31人阅读 评论(0) 收藏
格式:test 测试条件 字符串测试: 注意空格: test str1 == str2 测试字符串是否相等 test str1 != str2 测试字符串是否不相等 test str1 测试字符串是否 ...
随机推荐
- shell 循环总结
#!/bin/bash my_arry=(a b "c","d" abc) echo "-------FOR循环遍历输出数组--------" ...
- Python day21模块介绍4(logging模块,configparser模块)
1.日志等级从上往下依次降低 logging.basicConfig(#日志报错打印的基础配置 level=logging.DEBUG, filename="logger.log" ...
- Oracle监听服务
Oracle数据库中的主要用户及其作用 No. 用户名 默认密码 描述 1 sys change_on_install 数据库的超级管理员 2 system manager 数据库的普通管理员 3 s ...
- MongoDB(课时19 数据删除)
3.4.4 删除数据 在MongoDB里面删除数据使用“remove()”.但是这个函数有两个可选项: 删除条件:满足条件的数据被删除. 只删除一个数据:设置为true或者是1表示只删除一个. 范例: ...
- Kali Linux 2016.2初体验
前言 Kali Linux官 方于8月30日发布Kali Linux 2016的第二个版本Kali Linux 2016.2.该版本距离Kali Linux 2016.1版本发布,已经有7个月.在这期 ...
- [原][OSG][osgBullet][osgworks][bullet]编译osgBullet尝试物理引擎
相关网址: 类似文章:http://blog.csdn.net/lh1162810317/article/details/17475297 osgBullet官网:http://osgbullet.v ...
- mac 无法打开xx ,因为无法确认开发者身份
系统偏好与设置 - 安全性与隐私 - 通用 允许从以下位置下载的应用: 选择 [任何来源],如果没有这个选项,使用终端执行下面的命令: spctl --master-disable (spctl空格 ...
- Rspec: everyday-rspec实操。FactoryBot预构件 (rspec-expectations gem 查看匹配器) 1-4章
总文档连接: RSpec.info/documentation/ 包括core, expectiation,rails , mock, 点击最新版本,然后右上角搜索class, method. 第3章 ...
- 3-9《元编程》第3章Tuesday:methods
第3章methods Ruby是动态语言,有静态语言实现不了的技巧.本章讲解代码的重构,把代码变得更简洁. 3.2Dynamic Methods 3.21Calling Methods Dynamic ...
- 『PyTorch』第七弹_nn.Module扩展层
有下面代码可以看出torch层函数(nn.Module)用法,使用超参数实例化层函数类(常位于网络class的__init__中),而网络class实际上就是一个高级的递归的nn.Module的cla ...