iOS学习笔记——触摸事件
2013-12-30 09:25
791 查看
iOS设备都是可以多点触摸的,是指手指放在iOS设备的屏幕上从屏幕上拖动或抬起。系统当前视图响应触摸事件,若无响应则向上层传递,构成响应者链。触摸事件的函数有4个。
创建一个视图,继承UIView类,在视图控制器中把视图加载到视图控制器上:
创建一个视图,继承UIView类,在视图控制器中把视图加载到视图控制器上:
- (void)viewDidLoad { [super viewDidLoad]; //创建一个视图对象,响应触摸动作 LinView * pView = [[LinView alloc]initWithFrame:CGRectMake(0, 0, 320, 450)]; //设置视图的背景颜色,与根视图区别 pView.backgroundColor = [UIColor blueColor]; //把视图添加到当前视图,或视图控制器 [self.view addSubview:pView]; //释放创建的对象 [pView release]; }在.h文件中添加成员变量:
@interface LinView : UIView { //创建一个成员,存放两点之间距离变化的信息 float _lastDistance; } @end在.h文件中对各类事件进行相关的实现、处理:
@implementation LinView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { //⭐️开启多点触摸 self.multipleTouchEnabled = YES; self.userInteractionEnabled = YES; } return self; } #pragma mark--------触摸开始时调用此方法 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //获取任意一个touch对象 UITouch * pTouch = [touches anyObject]; //获取对象所在的坐标 CGPoint point = [pTouch locationInView:self]; //以字符的形式输出触摸点 NSLog(@"触摸点的坐标:%@",NSStringFromCGPoint(point)); //获取触摸的次数 NSUInteger tapCount = [pTouch tapCount]; //对触摸次数判断 if (tapCount == 1) { //在0.2秒内只触摸一次视为单击 [self performSelector:@selector(singleTouch:) withObject:nil afterDelay:0.2]; } else if(tapCount == 2) { //取消单击响应,若无此方法则双击看做是:单击事件和双击事件 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTouch:) object:nil]; //确定为双击事件 [self doubleTouch:nil]; } } //单击方法 - (void)singleTouch:(id)sender { //对应单击时发生的事件 NSLog(@"此时是单击的操作"); } //双击方法 - (void)doubleTouch:(id)sender { //双击时对应发生的事件 NSLog(@"此时是双击的操作"); } #pragma mark----------触摸的移动(滑动) - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { //获取所有的触摸对象 NSArray * array = [touches allObjects]; //分别取出两个touch对象 UITouch * pTouch1 = [array objectAtIndex:0]; UITouch * pTouch2 = [array objectAtIndex:1]; //获取两个touch对象的坐标点 CGPoint point1 = [pTouch1 locationInView:self]; CGPoint point2 = [pTouch2 locationInView:self]; //用封装的方法计算两触摸点之间的距离 double distance = [self distanceOfPoint:point1 withPoint:point2]; //判断两点间距离的变化 if ((distance - _lastDistance) > 0) { //两点距离增大的方法,一般为图片、文字等的放大,捏合 NSLog(@"两点距离变大"); } else { //两点距离减小的方法,一般为图片、文字等的缩小,放开 NSLog(@"两点距离变小"); } //把现在的距离赋值给原来的距离,覆盖之 _lastDistance = distance; } //编写一个计算两点之间距离的方法,封装此方法,方便调用 - (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; } #pragma mark--------手离开屏幕,触摸事件结束 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { //触摸结束时发生的事件 } #pragma mark--------触摸事件被打断,比如电话打进来 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { //一般不写此方法,可以把程序挂起,放在后台处理 } @end
相关文章推荐
- iOS学习笔记--触摸事件
- iOS学习笔记-iOS事件处理之触摸事件
- iOS 学习笔记 触摸事件
- iOS学习笔记之触摸事件&UIResponder
- iOS学习笔记05-触摸事件
- IOS学习笔记9—IOS触摸事件监听和操作
- iOS学习笔记05-触摸事件
- iOS学习笔记-062.触摸事件
- ios自用学习笔记-触摸事件与手势识别
- iOS学习笔记05-触摸事件
- iOS学习笔记--触摸事件
- iOS开发学习之触摸事件和手势识别
- iOS学习笔记-065.事件的传递
- cocos2d-x 2.X demo学习笔记 6 ----ClickAndMoveTest 触摸事件和移动
- iOS学习笔记(2)— UIView用户事件响应
- IOS学习笔记之触摸与手势
- iOS开发学习之触摸事件和手势识别
- iOS开发学习之触摸事件和手势识别
- iOS学习之触摸事件
- iOS开发学习笔记 -- (二)动态添加控件和事件处理