1.触摸事件的处理方式
(1)事件类型
触摸事件(触摸手机屏幕)
运动事件(如微信的摇一摇)
远程控制事件(如蓝牙)
(2)响应者类通过复写以下方法,可以监听触摸事件
//当一个或多个手指触碰屏幕时
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
// 当一个或多个手指在屏幕上移动时
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
// 当一个或多个手指离开屏幕时
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
//当触摸序列被诸如电话呼入这样的系统事件所取消时
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
(3)UITouch触摸对象
当用户触摸视图时,调用视图如下方法:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
NSInteger tapNumber = [touch tapCount];
}
当用户触摸屏幕时,事件会被封装成一个event实例,
包含了触摸事件相关的信息,event实例包含着若干个UITouch实例,一个touch表示一个手指.
UITouch类中的常用属性:
window:触摸时产生所处的窗口 view::触摸产生时所在的视图
tapCount:tapCount表示短时间内轻击屏幕的次数.(可根据该属性判断单击,双击或更多)
phase:触摸事件在屏幕上的一个周期,即触摸开始,触摸点移动,触摸结束,还有中途取消,
phase可以查看当前触摸事件在一个周期中所处的状态, phase的类是 UITouchPahse,其枚举型包含:
UITouchPahseBegan(触摸开始) UITouchPahseMoved(接触点移动)
UITouchPahseStationary(接触点无移动)
UITouchPahseEnded(触摸结束)
UITouch类中的常用方法:
//函数返回一个CGpoint类型的值,表示触摸在view上的位置,返回的位置是针对view的坐标系
- (CGPoint)locationInView:(UIView *)view CGPointview view;
//记录了前一个坐标值
- (CGPoint)previousLocationInView:(UIView *)view CGPoint ;
2.事件传递的过程
事件的传递:
从事件发生到其处理的对象,传递要经过特殊的一
段过程。当用户点击设备屏 幕时,iOS捕捉到一系
列的触摸,将其打包到UIEvent对象中并放置到应
用程序 活动事件队列中。UIApplication对象
从事件队列中取出最前面的事件并将其分 发。通
常,其发送事件给应用程序的主窗口——UIWindow
实例,再由窗口对 象发送事件给『第一响应者
(触摸的视图)』处理。
3.响应者链的基本概念
响应者对象是一个能接收并处理事件的对象。
UIResponser是所有响应者对 象的基类。该基
类定义了一系列编程接口,不但为事件处理进行服
务而且还提 供了通用的响应行为处理。
UIApplication, UIView(包括UIWindow),
UIViewController都直接或间接的继承自
UIResponser,所有的这些类的实例 都是响应者
对象。
事件响应者链:
当用户与视图交互时,将会 消息传递给视图控制器,如果 不存在控制器,传递给父视图.
如果不处理该消息,则继续 将消息向上传递
最上层的视图如果也不处理, 将事件交予Window对象
最后交由UIApplication实例, 如果不处理,丢弃事件
事件分发和响应过程如下图:
事件的拦截:
userInteractionEnable = NO;
hidden = YES;
alpha = 【0——0.01】范围
4.手势识别
UIGestureRecognizer:
UIGestureRecognizer类,用于检测、识别用户
使用设备时所用的手势。它是一 个抽象类,定义了
所有手势的基本行为。以下是
UIGestureRecognizer子类,用 于处理具体的用
户手势行为:
- UITapGestureRecognizer(轻击)
- UIPinchGestureRecognizer(捏合)
- UIPanGestureRecognizer(平移)
- UISwipeGestureRecognizer(轻扫)
- UIRotationGestureRecognizer(旋转)
- UILongPressGestureRecognizer(长按)
//1 单击
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
[imageV addGestureRecognizer:tap];
//2 双击
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTapAction:)];
doubleTap.numberOfTapsRequired = 2;
[imageV addGestureRecognizer:doubleTap];
//双击失败了才算单击(双击成功就是双击)
[tap requireGestureRecognizerToFail:doubleTap];
//3 长按
//allowableMovement:允许抖动的范围
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
[imageV addGestureRecognizer:longPress];
//4 轻扫
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
//属性:设置轻扫的方向(默认 右)
// swipe.direction = UISwipeGestureRecognizerDirectionRight;
[imageV addGestureRecognizer:swipe];
//5 移动
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
//轻扫和移动的冲突,一旦有冲突,默认移动
//轻扫失败算移动 (轻扫成功就是轻扫)
[pan requireGestureRecognizerToFail:swipe];
//6 旋转
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
[imageV addGestureRecognizer:rotation];
//7 捏合
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
[imageV addGestureRecognizer:pinch];
以上方法的实现:
-(void)pinchAction:(UIPinchGestureRecognizer*)pinch{
/*属性
scale:缩放的大小比例
velocity:缩放的速度
*/
if (pinch.state ==UIGestureRecognizerStateChanged) {
NSLog(@"一直捏合");
CGFloat pScale =pinch.scale;
pinch.view.transform = CGAffineTransformMakeScale(pScale, pScale);
}else if (pinch.state == UIGestureRecognizerStateEnded){
NSLog(@"捏合结束");
[UIView animateWithDuration:0.2 animations:^{
pinch.view.transform =CGAffineTransformIdentity;
}];
}
}
-(void)rotationAction:(UIRotationGestureRecognizer*)rotation{
/*
rotation:旋转的弧度 角度:0~360 弧度:0~2π
velocity:旋转的速度
*/
if (rotation.state == UIGestureRecognizerStateChanged) {
NSLog(@"一直旋转");
CGFloat angle = rotation.rotation;
rotation.view.transform = CGAffineTransformMakeRotation(angle);
}else if (rotation.state == UIGestureRecognizerStateEnded){
NSLog(@"旋转结束");
[UIView animateWithDuration:0.2 animations:^{
rotation.view.transform = CGAffineTransformIdentity;
}];
}
}
-(void)panAction:(UIPanGestureRecognizer*)pan{
// NSLog(@"移动...");
//设置当前位置的坐标(相对 起始点 )
CGPoint p1 =[pan translationInView:imageV];
NSLog(@"p1 = %@",NSStringFromCGPoint(p1));
//移动点的速度
// CGPoint v1 = [pan velocityInView:imageV];
// NSLog(@"v1 = %@",NSStringFromCGPoint(v1));
//设置当前位置
// CGPoint p2 = CGPointMake(100, 100);
// [pan setTranslation:p2 inView:imageV];
}
-(void)swipeAction:(UISwipeGestureRecognizer*)swipe{
NSLog(@"轻扫");
}
-(void)longPressAction:(UILongPressGestureRecognizer*)longPress
{
// NSLog(@"长按");
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按开始");
}else if (longPress.state ==UIGestureRecognizerStateEnded){
NSLog(@"长按结束");
}
}
-(void)tapAction:(UITapGestureRecognizer*)tap{
NSLog(@"单击");
}
-(void)doubleTapAction:(UITapGestureRecognizer*)tap{
NSLog(@"双击");
}