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 测试字符串是否 ...
随机推荐
- mui --- 怎么获取百度地图定位功能
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- Yandex.Algorithm 2011 Round 2 D. Powerful array 莫队
题目链接:点击传送 D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input ...
- c++ 判断容器A是否是容器B的子集,如果是,返回true(includes)
#include <iostream> // cout #include <algorithm> // includes, sort using namespace std; ...
- shell 脚本拼接
var21=`echo $vvar|awk -F ',' '{print $1}'` echo $var21 var31=`echo $var21|awk -F ':' '{print $2}'` e ...
- 《剑指offer》第三十二题(分行从上到下打印二叉树)
// 面试题32(二):分行从上到下打印二叉树 // 题目:从上到下按层打印二叉树,同一层的结点按从左到右的顺序打印,每一层 // 打印到一行. #include <cstdio> #in ...
- 修改unity变量名但不丢失序列化值
using UnityEngine; using UnityEngine.Serialization; public class LgsTest : MonoBehaviour { [Formerly ...
- codeforces 576a//Vasya and Petya's Game// Codeforces Round #319 (Div. 1)
题意:猜数游戏变种.先选好猜的数,对方会告诉你他想的那个数(1-n)能不能整除你猜的数,问最少猜几个数能保证知道对方想的数是多少? 对一个质数p,如果p^x不猜,那么就无法区分p^(x-1)和p^x, ...
- Confluence 6 从外部目录中同步数据支持的目录类型
针对一些特定的用户目录类型,Confluence 在系统的数据库中保存了目录的缓存信息(用户和用户组),这样能够让系统更快速的访问用户和用户组数据.一个数据同步的进程将会间歇性的在系统中运行来将远程的 ...
- Confluence 6 使用 LDAP 授权连接一个内部目录 - 用户组 Schema 设置
请注意:这部分仅在拷贝用户登录(Copy User on Login)和 同步组成员(Synchronize Group Memberships)被启用后可见. 其他用户组 DN(Additional ...
- boruvka算法
算法正确性证明: 1.最优性:最小边一定包含在生成树中. 2.合法性:一定不会构成环.如果存在环说明一个点的最小连边有两个,显然矛盾. 算法时间复杂度证明: 每执行一次算法,所有联通块的大小都至少为2 ...