一、事件的基本概念
1.事件概述
UIEvent:事件,是由硬件捕捉的一个表示用户操作设备的对象。 分三类:触摸事件、晃动事件、远程控制事件
触摸事件:用户通过触摸设备屏幕操作对象、输入数据。支持多点触摸,包含1个到多个触摸点
二、触摸的基本概念
实现触摸
UIView支持触摸事件(因为继承于UIResponder),而且支持多点触摸。
需要定义UIView子类,实现触摸相关的方法。
touches..began、touches..moved、touches…ended、 touches..canceled。
实现顺序:
1.新建TouchView继承于UIView
2.在根视图控制器中创建TouchView
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
// 需要让TouchView 来处理事件(响应事件)
// 实现响应者类中的方法 来捕获事件
TouchView *touchView = [[TouchView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
touchView.backgroundColor = [UIColor redColor];
[self.view addSubview:touchView];
[touchView release];
}
3.在TouchView.m中添加
// 触摸开始
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@