第四讲 事件处理本讲内容

一. 事件的基本概念
UIEvent: 事件,是由硬件捕捉的一个表示用户操作设备的对象
分三类: 触摸事件、晃动事件、远程控制事件
触摸事件: 用户通过触摸设备屏幕操作对象、输入数据。支持多点触摸,包含1个到多个触摸点

二. 触摸的基本概念
UIView支持触摸事件(因为继承于UIResponder),而且支持多点触摸
需要定义UIView子类,实现触摸相关的方法: touches..began、touches..moved、touches…ended、 touches..canceled
使用触摸实现手势
手势: 有规律的触摸
UITouch代表触摸在屏幕上的一根手指。可以获取触摸时间和触摸位置
如何获取touch对象。touches集合中包含了视图上的所有手势
// 定义视图类, 如何实现轻扫?
三. 响应者链–由多个响应者对象组成的链

UIResponder: 响应者类.
iOS中所有能响应事件(触摸、晃动、远程事件)的对象都是响应 者
系统定义了一个抽象的父类UIResponder来表示响应者。其子类都是响应者

检测触碰视图
硬件检测到触摸操作,会将信息交给UIApplication,开始检测
UIApplication -> window -> viewController -> view -> 检测所有子视图
最终确认触碰位置,完成响应者链的查询过程

处理触碰事件
检测到响应者后,实现touchesBegan:withEvent:等方法,即处理事件
如果响应者没有处理事件,事件会向下传递。如果没有响应者处理,则丢弃触摸事件
事件处理的顺序与触摸检测查询相反
触摸的子视图 -> view -> viewController -> window -> UIApplication

阻断响应者链
响应者链可以被打断。无法完成检测查询过程
视图类的属性 : userInteractionEnabled。关闭后能阻断查询过 程

#import "AppDelegate.h"@interface AppDelegate ()@end@implementation AppDelegate- (void)dealloc
{[_window release];[super dealloc];
}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];self.window.backgroundColor = [UIColor whiteColor];[self.window makeKeyAndVisible];[_window release];self.window.backgroundColor = [UIColor blackColor];UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
//    label.backgroundColor = [UIColor cyanColor];[self.window addSubview:label];label.userInteractionEnabled = YES;[label release];label.alpha = 0.5;UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];button.frame = CGRectMake(0, 25, 50, 50);button.backgroundColor = [UIColor redColor];[label addSubview:button];[button addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];button.userInteractionEnabled = YES;// 只有当标签和按钮的用户交互都启用时,按钮的点击效果才可用,而且label上铺button只要label有背景颜色,就看不见按钮return YES;
}- (void)test
{NSLog(@"测试");
}@end

视图控制器

#import "MainViewController.h"
#import "MyView.h"
@interface MainViewController ()@property(nonatomic, retain)UITextField *myTextField;@end@implementation MainViewController- (void)dealloc
{[self.myTextField release];[super dealloc];
}- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];if (self) {
//        一般放入对容器的初始化}return self;
}- (void)loadView
{[super loadView];
}- (void)viewDidLoad {[super viewDidLoad];self.myTextField = [[UITextField alloc] initWithFrame:CGRectMake(50, 50, 150, 40)];self.myTextField.layer.cornerRadius = 10;self.myTextField.layer.borderWidth = 1;[self.view addSubview:self.myTextField];[self.myTextField release];// 创建一个MyViewMyView *myView = [[MyView alloc] initWithFrame:CGRectMake(100, 200, 150, 150)];myView.backgroundColor = [UIColor blackColor];[self.view addSubview:myView];[myView release];// 主要为了掌握好视图大小,避免控件超出视图范围外,造成失效UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];view.backgroundColor = [UIColor magentaColor];[self.view addSubview:view];[view release];UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];button1.frame = CGRectMake(0, 0, 100, 100);button1.layer.borderWidth = 1;button1.layer.cornerRadius = 5;[button1 addTarget:self action:@selector(click1:) forControlEvents:UIControlEventTouchUpInside];[view addSubview:button1];// 像这个button按钮一般在父视图上一般在外面,也就只有一半的面积点击按钮有效UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];button2.frame = CGRectMake(150, 100, 100, 100);button2.layer.borderWidth = 1;button2.layer.cornerRadius = 5;[button2 addTarget:self action:@selector(click2:) forControlEvents:UIControlEventTouchUpInside];[view addSubview:button2];// ViewController中的初始化方法,loadView,viewDidLoad只会运行一次,但是viewAppear只要视图显示,就会执行一次
}- (void)click1:(UIButton *)button
{NSLog(@"打印成功");
}- (void)click2:(UIButton *)button
{NSLog(@"打印成功");
}// 触摸
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{NSLog(@"触摸开始");// 点击空白处 回收键盘[self.myTextField resignFirstResponder];
}- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{NSLog(@"触摸移动");
}- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{NSLog(@"触摸被取消");    // 这个怎么打印出来?// 应该是使用视图类的属性userInteractionEnabled,设置成NO,即关闭阻断查询过程,阻断响应者链,使其无法完成检测查询过程
}- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{NSLog(@"触摸结束");
}// 摇一摇, 测试模拟器时没有办法摇晃...选中模拟器鼠标点击上方软件选项中的Hardware->Shake Gesture
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{NSLog(@"摇一摇开始");self.view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
}- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{NSLog(@"摇一摇结束");
}- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{NSLog(@"摇一摇被取消");
}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];NSLog(@"内存警告");
}@end

自定义视图

#import "MyView.h"// 补上延展部分,为的是防止外部访问
@interface MyView ()// 用来记录视图的开始坐标
@property(nonatomic, assign)CGPoint startPoint;@end@implementation MyView- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{// 集合里元素个数
//    NSLog(@"%ld", touches.count);   // 始终为1// 集合里有一个触摸类的对象UITouch *touch = [touches anyObject];// 通过触摸对象获取相应视图的当前位置self.startPoint = [touch locationInView:self];NSLog(@"%g", self.startPoint.x);NSLog(@"%g", self.startPoint.y);NSLog(@"触摸开始了");
}- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{self.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];// 通过移动,找到变化,然后让MyView也进行相应的调整,从而实现视图随触摸位置移动的效果// 获取触摸对象UITouch *touch = [touches anyObject];// 获取移动之后的坐标CGPoint movedPoint = [touch locationInView:self];// 坐标的变化CGFloat dx = movedPoint.x - self.startPoint.x;CGFloat dy = movedPoint.y - self.startPoint.y;self.center = CGPointMake(self.center.x + dx, self.center.y + dy);NSLog(@"触摸移动中");
}- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{NSLog(@"触摸已结束");
}@end

UITouch 摇晃手势启动器相关推荐

  1. ios各种手势,很有意思

    转自http://blog.csdn.net/likendsl/article/details/7554150 这哥们很厉害的 一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由U ...

  2. 安卓自定义音量键_红米2|LineageOS17.0|安卓10.0|2.4编译|顶级流畅|超强跑分|全新手势|强力推荐...

    红米2|LineageOS17.0|安卓10.0|2.4编译|顶级流畅|超强跑分|全新手势|强力推荐 第三方ROM不保证无bug下载需谨慎,但是真的是非常好用 ★★刷机需知★★ .务必解锁BL!务必解 ...

  3. 安卓自定义音量键_红米6Pro|LineageOS17.1|安卓10.0|0125编译|顶级流畅|超强跑分|全新手势|...

    红米6Pro|LineageOS17.1|安卓10.0|0125编译|顶级流畅|超强跑分|全新手势|强力推荐 第三方ROM不保证无bug下载需谨慎,但是真的是非常好用 ★★刷机需知★★ .务必解锁BL ...

  4. 乐视x820android最新版本,乐视MAX2|MIUI10|安卓8.1|最终完美版|极速_最新最全的乐Max2ROM刷机包下载、刷机教程_...

    因为Magisk的关系开机会有系统提示完全不影响 精简桌面数据库,去掉残留图标 适度精简系统应用APK.删除无用SO库运行文件 提高全局触摸响应 提高屏幕滑动灵敏度 优化桌面响应速度 已开启内核对支持 ...

  5. 乐视x820android最新版本,乐视MAX2|MIUI10|安卓8.1|最终完美版极速流_最新最全的乐Max2ROM刷机包下载、刷机教程...

    因为Magisk的关系开机会有系统提示完全不影响 精简桌面数据库,去掉残留图标 适度精简系统应用APK.删除无用SO库运行文件 提高全局触摸响应 提高屏幕滑动灵敏度 优化桌面响应速度 已开启内核对支持 ...

  6. 乐2的android版本,乐视2高通|MIUI10|安卓6.0|最终完美版|极速_最新最全的乐2高通版ROM刷机包下载、刷机...

    乐视2高通|MIUI10|安卓6.0|最终完美版|极速流畅|稳定实用|摇晃手势|DDK设置|养老专用 更新编译工具ROM体积更小相机切换不卡 因为Magisk的关系开机会有系统提示完全不影响 精简桌面 ...

  7. 新书推荐:iOS Swift 游戏开发指南

    章节目录如下: 第 1 章 游戏的设计 - 9 1.1 设计游戏引擎 -9 1.2 创建基于继承的设计-10 1.3 创建基于组件的设计-12 1.4 用 GameplayKit 创建基于组件的游戏设 ...

  8. UndoManager教程

    原文:UndoManager Tutorial: How to Implement With Swift Value Types 作者:Lyndsey Scott 译者:kmyhy 注: 本教程基于 ...

  9. mix2s适配鸿蒙,小米MIX2S|MIUI10|9.05.12|GPU调节|CPU调节_最新最全的小米MIX 2SROM刷机包下载、刷机...

    小米MIX2S|MIUI10|9.05.12|GPU调节|CPU调节|摇晃手势|屏幕助手|悬浮助手|高级设置 ★★刷机需知★★ 记得一定要格式化DATA不然会出现没有高级设置如出现Magisk未安装的 ...

最新文章

  1. 哈工大导师禁止实验室硕士出去实习,称「实习就像和35岁渣男试婚」,你怎么看?...
  2. 栈与队列5——汉诺塔问题
  3. 音视频技术开发周刊 | 238
  4. javascript真的是异步的吗?且看setTimeout的实现原理以及setTimeout(0)的使用场景
  5. 电脑存储:A盘、B盘知识介绍,为何总是电脑磁盘从C盘开始?
  6. MONyog-数据库性能监控工具
  7. 机器学习介绍jc01
  8. mysql之调优概论 1
  9. c#代码片段新建(sinppet)
  10. .net中的装箱与拆箱!
  11. 小米浏览器禁止java,如何禁止小米手机浏览器中弹出窗口广告
  12. Faster RCNN与Mask RCNN
  13. 传智播客python培训视频教程下载
  14. Android热修复之Sophix初探
  15. docker deamon源码学习
  16. 编辑中的word变成只读_打开Word文件是只读,怎么修改
  17. Vue动态加载本地磁盘图片
  18. 仙境传说 第一章之四 卢渊*梦魇过后的情缘
  19. 劈开迷雾:蘑菇街搜索架构及搜索排序实践
  20. 2022-2023年度江苏省职业院校技能大赛“网络安全”赛项中职组圆满成功

热门文章

  1. 使用 Moco 搭建一个模拟服务器
  2. digit puzzle 数字谜 Uva12107
  3. 博客备份工具BlogDown 软件使用感想
  4. 追猎者:因感恩节美盘休市贵金属市场交投清单
  5. 追猎者:贵金属二次探底未果,本周依然保持逢低做多
  6. vnc怎么控制linux系统,LINUX下使用VNC进行图形界面远程桌面控制有哪些呢?
  7. 单片机开发综合实验装置
  8. 玩转 Tanzu Community Edition(社区版)
  9. 从代码角度理解什么是蜜罐
  10. 抛物型方程的有限差分 C语言程序,抛物型方程有限差分方法的应用 - 报告.doc