2013-12-19 21:59 171人阅读 评论(0) 收藏 举报
事件产生与分
触摸
手势
手势识别器。
事件产生与分发:ios中的触摸事件,基于多点触摸模型
多点触摸序列:
一个或多个接触屏幕的手指,识别为多点触摸序列的一部分
从第一个手指碰到屏幕开始,直到最后一个手指离开屏幕结束
Touch 是指手指放在屏幕上。
触摸数量等于手指数量。
手指触摸屏幕、立即离开,则出发tap。
手势————
UITouch:一个触摸
UIEvent:一个事件
响应者链————
——》第一响应者是正在交互单对象
——》第一响应者不处理事件,事件会按照响应者链逐级发送
——》事件最终发送到uiapplication,若不处理,则被丢弃
事件处理函数
-toucheBegan:withEvent:
当用户开始触摸屏幕时,在事件的开始阶段调用
-toucheMoved:withEvent:
处理手指的移动
-toucheEnded:withEvent:
结束触摸过程时调用
-toucheCancelled:withEvent:
触摸事件被系统事件(呼入电话等)中断时调用
实现触摸处理:
新建单视图工程(TouchExplorer)实现触摸处理。
XIB文件配置:在界面上布置两个标签,实现链接
开启多点触摸相关代码:self.multipleTouchEnabled=YES;
(self.userInteractionEnabled=YES;)->可省
获取一个touch对象的相关代码 UITouch *pTouch=[touches anyObject];
获取对象在坐标系中的点相关代码:
CGPoint point=[pTouch locationInView:self];
获取点击次数相关代码: NSInteger tapCount=[pTouch tapCount];
假设点击一次关联方法为- (void)singleTouch:(id)sender;点击两次关联方法为
- (void)doubleTouch:(id)sender,则有如下:
一、 实现单次点击晚0.2秒执行代码: [self performSelector:@selector(singleTouch:) withObject:nil afterDelay:0.2];
二、 实现双击,然后并不执行单击方法相关代码: [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTouch:) object:nil];
计算两点之间距离的方法:(封装方法)
- (double)distanceOfPoint:(CGPoint)point1 withPoint:(CGPoint)point2{
double num1=pow(point1.x - point2.x,2);
double num2=pow(point1.y - point2.y,2);
double distance=sqrt(num1+num2);
return distance;
}
利用自己封装好的计算距离方法来求出实例中两点间单距离:
//获取所有触摸对象
NSArray *pArr=[touches allObjects];
//分别取出两个touch对象
UITouch *pTouch1=[pArr objectAtIndex:0];
UITouch *pTouch2=[pArr objectAtIndex:1];
//获取两个touch对象的坐标点
CGPoint point1=[pTouch1 locationInView:self];
CGPoint point2=[pTouch2 locationInView:self];
//通过自己封装的方法获取两个点之间的距离
double distance=[self distanceOfPoint:point1 withPoint:point2];
手势识别器
识别单击的相关代码(包括创建单击对象以及关联方法)下同:
UITapGestureRecognizer *pSingleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleGesture:)];
[self.view addGestureRecognizer:pSingleTap];
识别双击单相关代码:
UITapGestureRecognizer *pDoubleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleGesture:)];
pDoubleTap.numberOfTapsRequired=2; (多加注意)
[self.view addGestureRecognizer:pDoubleTap];
识别清扫手势相关代码:
UISwipeGestureRecognizer *pSwip=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipGesture:)];
//滑动方向(一定不要忘)
pSwip.direction=UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:pSwip];
[pSwip release];
识别平移相关代码:
UIPanGestureRecognizer *pPan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)];
[self.view addGestureRecognizer:pPan];
[pPan release];
(注意,加完平移代码后会覆盖上面加载的清扫代码,也就是不会再识别清扫)
识别长按相关代码:
UILongPressGestureRecognizer *pLong=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longGesture:)];
pLong.minimumPressDuration=1;(注意此步不能少)、、长按的时间间隔
[self.view addGestureRecognizer:pLong];
[pLong release];
识别旋转试图器相关代码:
UIRotationGestureRecognizer *pRotation=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGesture:)];
[self.view addGestureRecognizer:pRotation];
[pRotation release];