事件处理

事件概述

UIEvent:事件,是由硬件捕捉的一个表示用户操作设备的对象

分三类:触摸事件\  晃动事件\ 远程控制事件

触摸事件:会包含1个到多个触摸点

实现触摸

UIView支持触摸事件(因为继承于UIResponder),而且支持多点触摸

需要定义UIView子类,实现触摸相关的方法

touches..begin     touches..moved    touches..ended     touches..canceled

使用触摸实现手势

手势:有规律的触摸

UITouch代表触摸在屏幕上的一根手指,可以获取触摸时间和触摸位置

如何获取touch对象,touches集合中包含了视图上的所有手势

实现触摸事件

//建工程,添加根视图控制器,根视图控制器中添加TouchView类,创建一个视图对象
//TouchView.m中代码 #import "TouchView.h" @implementation TouchView
-(void)dealloc
{
[super dealloc];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// NSLog(@"%s %d",__FUNCTION__,__LINE__);
// NSLog(@"开始");
// //获得手指(触摸)对象,UITouch类在MVC中属于M,用于存储数据,并提供了一些方法,获取这些存储的数据,存储的数据很多,例如:点击时间,点击次数,点击的位置,上一次点击位置等等,详情见UITouch API.
// UITouch *touch = [touches anyObject];
//
// //获取 手指 在 指定视图 中得位置
// CGPoint currentPoint = [touch locationInView:self.superview];
// self.center = currentPoint;
// //把CGPoint转换为NSString 并打印出来
// NSLog(@"%@",NSStringFromCGPoint(currentPoint)); // NSLog(@"%@",touch);
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"%s %d",__FUNCTION__,__LINE__);
NSLog(@"移动");
//获得手指(触摸)对象,UITouch类在MVC中属于M,用于存储数据,并提供了一些方法,获取这些存储的数据,存储的数据很多,例如:点击时间,点击次数,点击的位置,上一次点击位置等等,详情见UITouch API.
UITouch *touch = [touches anyObject]; //获取 手指 在 指定视图 中得位置
CGPoint currentPoint = [touch locationInView:self.superview];
//获取手指在指定视图的上一个位置
CGPoint previousPoint = [touch previousLocationInView:self.superview];
//计算偏移点
CGPoint point = CGPointMake(currentPoint.x - previousPoint.x, currentPoint.y - previousPoint.y);
//center 在偏移后的center值
self.center = CGPointMake(self.center.x+point.x, self.center.y + point.y);
[self.superview bringSubviewToFront:self];
// self.center = point2;
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"%s %d",__FUNCTION__,__LINE__);
NSLog(@"取消");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGFloat red = arc4random()% / 255.0; //是float类型,除法运算需要加0
CGFloat green = arc4random() % / 255.0;
CGFloat blue = arc4random() % /255.0;
NSInteger width = self.frame.size.width;
NSInteger height = self.frame.size.height;
CGFloat x = arc4random()%( - width +) + width/;//center的x坐标随机
CGFloat y = arc4random()%( - width +) + height/;//center的y坐标随机 NSLog(@"%s %d",__FUNCTION__,__LINE__);
NSLog(@"结束");
self.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:];
self.center = CGPointMake(x, y); } - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
} /*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/ @end

轻扫事件

//SwipeView.m中代码
#import "SwipeView.h" @implementation SwipeView
-(void)dealloc
{
[super dealloc];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//设置支持多点触摸.默认是不支持的
self.multipleTouchEnabled = YES;
// Initialization code
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"%@",touches);
UITouch *touch = [touches anyObject];
_began = [touch locationInView:self]; }
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
_end = [touch locationInView:self];
CGFloat dx = _end.x - self.began.x;
CGFloat dy = _end.y - self.began.y;
if (dx> && fabs(dy)<) {
NSLog(@"向右轻扫");
self.superview.backgroundColor = [UIColor redColor];
}else if (dx<- && fabs(dy)<)
{
NSLog(@"向左轻扫");
self.superview.backgroundColor = [UIColor orangeColor];
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{ }
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{ }
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/ @end

响应者链

由多个响应者对象组成的链

UIResponder---响应者类

iOS中所有能响应事件(触摸  晃动 . 远程事件)的对象都是响应者

系统定义了一个抽象的父类UIResponder来表示响应者.其子类都是响应者

检测触碰视图

硬件检测到触摸操作,会将信息交给UIApplication,开始检测

UIApplication -> window -> viewController -> view -> 检测所有⼦视图

最终确认触碰位置,完成响应者链的查询过程。

处理触碰事件

检测到响应者后,实现touchesBegan:withEvent:等⽅法,即处理事件。

如果响应者没有处理事件,事件会向下传递。如果没有响应者处理,则丢弃触摸事件。

事件处理的顺序与触摸检测查询相反。

触摸的⼦视图 -> view -> viewController -> window -> UIApplication

阻断响应者链

响应者链可以被打断.无法完成检测查询过程

视图类的属性:userInteractionEnabled.关闭后能阻断查询过程

是视图的属性,控制器不能使用

UI学习笔记---第四天的更多相关文章

  1. python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例

    python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例 新浪爱彩双色球开奖数据URL:http://zst.aicai.com/ssq/openInfo/ 最终输出结果格 ...

  2. X-Cart 学习笔记(四)常见操作

    目录 X-Cart 学习笔记(一)了解和安装X-Cart X-Cart 学习笔记(二)X-Cart框架1 X-Cart 学习笔记(三)X-Cart框架2 X-Cart 学习笔记(四)常见操作 五.常见 ...

  3. opencv学习笔记(四)投影

    opencv学习笔记(四)投影 任选了一张图片用于测试,图片如下所示: #include <cv.h> #include <highgui.h> using namespace ...

  4. C++Primer第5版学习笔记(四)

    C++Primer第5版学习笔记(四) 第六章的重难点内容         你可以点击这里回顾第四/五章的内容       第六章是和函数有关的知识,函数就是命名了的代码块,可以处理不同的情况,本章内 ...

  5. Asp.Net Core WebApi学习笔记(四)-- Middleware

    Asp.Net Core WebApi学习笔记(四)-- Middleware 本文记录了Asp.Net管道模型和Asp.Net Core的Middleware模型的对比,并在上一篇的基础上增加Mid ...

  6. VSTO学习笔记(四)从SharePoint 2010中下载文件

    原文:VSTO学习笔记(四)从SharePoint 2010中下载文件 上一次我们开发了一个简单的64位COM加载项,虽然功能很简单,但是包括了开发一个64位COM加载项的大部分过程.本次我们来给CO ...

  7. Python学习笔记(四)

    Python学习笔记(四) 作业讲解 编码和解码 1. 作业讲解 重复代码瘦身 # 定义地图 nav = {'省略'} # 现在所处的层 current_layer = nav # 记录你去过的地方 ...

  8. deepin linux学习笔记(四)进不去图形界面怎么办?

    目录 deepin linux学习笔记(四)进不去图形界面怎么办? 前言 更换成lxde桌面 进不去图形界面怎么办? 总结 deepin linux学习笔记(四)进不去图形界面怎么办? 前言 生命不息 ...

  9. HTTP协议学习笔记(四)

    HTTP协议学习笔记(四) 与 HTTP 协作的 Web 服务器 一台 Web 服务器可搭建多个独立域名的 Web 网站,也可作为通信路径上的中转服务器提升传输效率. 1.用单台虚拟主机实现多个域名 ...

随机推荐

  1. 配置coffeeScript

    1.安装好node.js后 在系统环境变量自动会设置好: 我安装在D:\Program Files文件夹中   也安装好了npm(node packges manager)   2.系统会自动配置np ...

  2. 如何在 CentOS 中设置 NTP 服务器

    网络时间协议(NTP)用来同步网络上不同主机的系统时间.你管理的所有主机都可以和一个指定的被称为 NTP 服务器的时间服务器同步它们的时间.而另一方面,一个 NTP 服务器会将它的时间和任意公共 NT ...

  3. CSS最常用和实用的技巧

    1.重置浏览器的字体大小重置浏览器的默认值 ,然后重设浏览器的字体大小你可以使用雅虎的用户界面重置的CSS方案 ,如果你不想下载9MB的文件,代码如下: body,div,dl,dt,dd,ul,ol ...

  4. Html11.09CSS层叠样式表内容整理

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. mapreduce job提交流程源码级分析(三)

    mapreduce job提交流程源码级分析(二)(原创)这篇文章说到了jobSubmitClient.submitJob(jobId, submitJobDir.toString(), jobCop ...

  6. ROS的文件系统 (二)

    ROS的文件系统的基本概念, ROS文件系统中的两个最基本的概念:Package和Manifest,即包和清单文件. Package是组织ROS代码的最基本单位,每一个Package都可以包括库文件, ...

  7. ionic build --release android

    ionic bulid android ionic build --release android keytool -genkey -v -keystore demo.keystore -alias ...

  8. 10 件在 PHP 7 中不要做的事情

    1. 不要使用mysql_函数 这一天终于来了,从此你不仅仅"不应该"使用mysql_函数.PHP 7 已经把它们从核心中全部移除了,也就是说你需要迁移到好得多的mysqli_函数 ...

  9. SharePoint 2013 开发——APP开发的考虑和建议

    博客地址:http://blog.csdn.net/FoxDave 需要考虑的方面: 1. 记得CSOM授予网站集及以下的权限,而场解决方案需要整个场的访问权限. 2. 由于应用程序是彼此完全独立 ...

  10. zoj1530 bfs

    //Accepted zoj1530 270ms 40008KB #include <cstdio> #include <cstring> #include <iostr ...