1.事件处理方法
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
// 默认NO 多点触摸
self.view.multipleTouchEnabled = YES;
// 默认view YES 交互关闭 imageView 默认关闭
//self.view.userInteractionEnabled = YES;
// 如果父视图的交互关闭,会影响到子视图
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(60, 100, 200, 35);
[button setTitle:@"action" forState:UIControlStateNormal];
[self.view addSubview:button];
}
/**
* 重复父类的方法
*/
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// UITouch -> 触摸 -> 手指头数量(触摸点数量)
//NSLog(@"开始触摸 : %d", [touches count]);
// 获取touch对象
UITouch *touch = [touches anyObject];
/*
NSLog(@"阶段 : %d", [touch phase]);
// 获取窗口对象
UIWindow *widow = [touch window];
*/
// 获取点击次数
NSUInteger tapCount = [touch tapCount];
NSLog(@"tap : %lu", (unsigned long)tapCount);
// 获取当前的位置
CGPoint location = [touch locationInView:self.view];
NSLog(@"location : %@", NSStringFromCGPoint(location));
}// 开始触摸
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
/*
UITouch *touch = [touches anyObject];
// 获取前一个位置
CGPoint previous = [touch previousLocationInView:self.view];
NSLog(@"previous : %@", NSStringFromCGPoint(previous));
*/
}// 开始移动
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"触摸结束");
}// 触摸结束
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"触摸被取消");
}// 触摸被取消,一定在触摸事件在执行时,才有取消的条件(比如电话)
</pre><pre name="code" class="objc">还学到了:
01.// 设置状态栏的背景风格
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
02.Button可以响应多个Target事件1
03.设置父视图的userInteractionEnable为NO,会影响到其子视图。和设置alpha类似,当视图透明时,其子视图也会发生变化。
</pre><pre name="code" class="objc">2.练习
- (void)viewDidLoad {
[super viewDidLoad];
self.view.multipleTouchEnabled = YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
/*
// 单击&双击 ; 双手单机&双手双击
if ([touch tapCount] == 2 && [touches count] == 2) {
// 取消上一个调用的方法
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTap) object:nil];
[self doubleTap];
}else if ([touch tapCount] == 1 && [touches count] == 2) {
<span style="white-space:pre"> </span>// 延迟
<span style="white-space:pre"> </span>[self performSelector:@selector(singleTap) withObject:nil afterDelay:0.2];
}
*/
// 获取当前位置
_precious = [touch locationInView:self.view];
}// 处理单机&双击事件
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint end = [touch locationInView:self.view];
<span style="white-space:pre"> </span>// 位差调用
if (fabs(end.x - _precious.x) > 100.f) {
NSLog(@"hehe");
}
}
#pragma mark - Private Methods
- (void)singleTap
{
NSLog(@"singleTap");
}
- (void)doubleTap
{
NSLog(@"doubleTap");
}
3.响应事件者链
01.事件传递
UIApplication对象从事件队列中取出最前面的事件并将其分发。通常,其发送事件交由应用程序的主窗口——UIWindow实例,再由窗口对象发送事件给“第一响应者(触摸的视图)”处理。
</pre><pre name="code" class="objc">- (void)viewDidLoad {
[super viewDidLoad];
// View
/*
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(60, 100, 200, 35);
[button setTitle:@"action" forState:UIControlStateNormal];
[button addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
*/
// GestureRecognizer
/*
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(test)];
[self.view addGestureRecognizer:tap];
*/
// 下一个响应者测试
/*
MyView *view = [[MyView alloc] initWithFrame:CGRectMake(60, 200, 200, 200)];
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];
*/
// imageView
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 100, 200, 200)];
imgView.userInteractionEnabled = YES;
imgView.backgroundColor = [UIColor redColor];
[self.view addSubview:imgView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(50, 100, 100, 35);
button.backgroundColor = [UIColor grayColor];
[button setTitle:@"action" forState:UIControlStateNormal];
[imgView addSubview:button];
}
- (void)test
{
NSLog(@"用户点击");
}
// 子类化窗口
#import "MyWindow.h"
@implementation MyWindow
- (void)sendEvent:(UIEvent *)event
{
NSLog(@"发糖");
[super sendEvent:event];
}// 所有的事件都是由app交给窗口分发出去的
@end
还学到:
001.有些情况可有多个窗口的存在(比如:在qq消息的提示位置也有一个窗口),但只能有一个keyWindow.
</pre><pre name="code" class="objc">2.响应事件者链
响应事件者链表示一系列的响应者对象。事件被交由第一响应者对象处理,如果第一响应者不处理,事件被沿着响应者链向上传递,交给下一个响应者(next responder),如果响应,事件就会被截获(处理)。
// 查看响应层次结构
id next = [[[[[[self nextResponder] nextResponder] nextResponder] nextResponder] nextResponder] nextResponder];
01.过程
</pre><pre name="code" class="objc" style="font-size: 14px;"><span style="font-family: Arial, Helvetica, sans-serif;">- (id)initWithFrame:(CGRect)frame </span><span style="font-family: Arial, Helvetica, sans-serif;">{</span>
self = [super initWithFrame:frame];
if (self) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.alpha = 0.01;
button.frame = CGRectMake(50, 100, 100, 35);
button.backgroundColor = [UIColor lightGrayColor];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"push" forState:UIControlStateNormal];
[self addSubview:button];
}
return self;
}
- (void)click:(UIButton *)sender
{
id next = [self nextResponder];
while (next != nil) {
NSLog(@"next : %@", next);
next = [next nextResponder];
if ([next isKindOfClass:[UIViewController class]]) {
UIViewController *vc = next;
DetailViewController *detail = [[DetailViewController alloc] init];
[vc.navigationController pushViewController:detail animated:YES];
break;
}
}
}
3.事件拦截
userInteractionEnabled = NO;
hidden = YES;
alpha[[0~0.01]范围];
4.手势识别器
</pre><pre name="code" class="objc">- (void)viewDidLoad {
[super viewDidLoad];
/**
* 单击、双击
*/
/*
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
// 次数(单击、双击)
singleTap.numberOfTapsRequired = 1;
// 触摸数量(手指头的数量)
singleTap.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:singleTap];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
// 次数(单击、双击)
doubleTap.numberOfTapsRequired = 2;
// 触摸数量(手指头的数量)
//doubleTap.numberOfTouchesRequired = 2;
[self.view addGestureRecognizer:doubleTap];
// 当双击时,单击失效
[singleTap requireGestureRecognizerToFail:doubleTap];
*/
/**
* 轻扫 不能一个手指识别多个方向。
*/
/*
UISwipeGestureRecognizer *swipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
swipe1.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipe1];
UISwipeGestureRecognizer *swipe2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
swipe2.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipe2];
*/
/**
* 轻扫
*/
/*
_panView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 40, 40)];
_panView.backgroundColor = [UIColor redColor];
[self.view addSubview:_panView];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self.view addGestureRecognizer:pan];
*/
/**
* 长按
*/
/*
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
longPressGesture.minimumPressDuration = 2.f;
[self.view addGestureRecognizer:longPressGesture];
*/
/**
* 旋转
*/
/*
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
[self.view addGestureRecognizer:rotationGesture];
*/
/**
* 捏合
*/
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
[self.view addGestureRecognizer:pinch];
}
- (void)singleTap:(UITapGestureRecognizer *)tap
{
// 得到坐标位置
NSLog(@"单击 : %@", NSStringFromCGPoint([tap locationInView:self.view]));
}
- (void)doubleTap:(UITapGestureRecognizer *)tap
{
NSLog(@"双击");
}
- (void)swipe:(UISwipeGestureRecognizer *)swipe
{
NSLog(@"direction : %ld", swipe.direction);
}
- (void)pan:(UIPanGestureRecognizer *)pan
{
CGPoint point = [pan locationInView:self.view];
_panView.center = point;
}// move
- (void)longPress:(UILongPressGestureRecognizer *)lon
{
if (lon.state == UIGestureRecognizerStateBegan) {
NSLog(@"开始 : %ld", lon.state);
}else if (lon.state == UIGestureRecognizerStateEnded) {
NSLog(@"结束 : %ld", lon.state);
}else if (lon.state == UIGestureRecognizerStateChanged) {
NSLog(@"移动 : %ld", lon.state);
}
}
- (void)rotation:(UIRotationGestureRecognizer *)rotate
{
<span style="white-space:pre"> </span> 角度。
<span style="white-space:pre"> </span>//float degress = rotate.rotation * (180 / M_PI);
}
- (void)pinch:(UIPinchGestureRecognizer *)pinch
{
NSLog(@"pinch : %.1f", pinch.scale);
if (pinch.velocity > 0) {
NSLog(@"放大");
}else {
NSLog(@"放小");
}
}
</pre><pre name="code" class="objc">