第四讲 事件处理
本讲内容
一. 事件的基本概念
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];
// 创建一个MyView
MyView *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