分享

触摸事件

 叹落花 2014-12-20

iOS设备都是可以多点触摸的,是指手指放在iOS设备的屏幕上从屏幕上拖动或抬起。系统当前视图响应触摸事件,若无响应则向上层传递,构成响应者链。触摸事件的函数有4个。

创建一个视图,继承UIView类,在视图控制器中把视图加载到视图控制器上:

  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     //创建一个视图对象,响应触摸动作  
  5.     LinView * pView = [[LinView alloc]initWithFrame:CGRectMake(0, 0, 320, 450)];  
  6.     //设置视图的背景颜色,与根视图区别  
  7.     pView.backgroundColor = [UIColor blueColor];  
  8.     //把视图添加到当前视图,或视图控制器  
  9.     [self.view addSubview:pView];  
  10.     //释放创建的对象  
  11.     [pView release];  
  12. }  
在.h文件中添加成员变量:

  1. @interface LinView : UIView  
  2. {  
  3.     //创建一个成员,存放两点之间距离变化的信息  
  4.     float _lastDistance;  
  5. }  
  6.   
  7. @end  
在.h文件中对各类事件进行相关的实现、处理:

  1. @implementation LinView  
  2.   
  3. - (id)initWithFrame:(CGRect)frame  
  4. {  
  5.     self = [super initWithFrame:frame];  
  6.     if (self) {  
  7.         //??开启多点触摸  
  8.         self.multipleTouchEnabled = YES;  
  9.         self.userInteractionEnabled = YES;  
  10.     }  
  11.     return self;  
  12. }  
  13. #pragma mark--------触摸开始时调用此方法  
  14. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  
  15. {  
  16.     //获取任意一个touch对象  
  17.     UITouch * pTouch = [touches anyObject];  
  18.     //获取对象所在的坐标  
  19.     CGPoint point = [pTouch locationInView:self];  
  20.     //以字符的形式输出触摸点  
  21.     NSLog(@"触摸点的坐标:%@",NSStringFromCGPoint(point));  
  22.     //获取触摸的次数  
  23.     NSUInteger tapCount = [pTouch tapCount];  
  24.     //对触摸次数判断  
  25.     if (tapCount == 1)  
  26.     {  
  27.         //在0.2秒内只触摸一次视为单击  
  28.         [self performSelector:@selector(singleTouch:) withObject:nil afterDelay:0.2];  
  29.     }  
  30.     else if(tapCount == 2)  
  31.     {  
  32.         //取消单击响应,若无此方法则双击看做是:单击事件和双击事件  
  33.         [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTouch:) object:nil];  
  34.         //确定为双击事件  
  35.         [self doubleTouch:nil];  
  36.     }  
  37. }  
  38. //单击方法  
  39. - (void)singleTouch:(id)sender  
  40. {  
  41.     //对应单击时发生的事件  
  42.     NSLog(@"此时是单击的操作");  
  43. }  
  44. //双击方法  
  45. - (void)doubleTouch:(id)sender  
  46. {  
  47.     //双击时对应发生的事件  
  48.     NSLog(@"此时是双击的操作");  
  49. }  
  50. #pragma mark----------触摸的移动(滑动)  
  51. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  
  52. {  
  53.     //获取所有的触摸对象  
  54.     NSArray * array = [touches allObjects];  
  55.     //分别取出两个touch对象  
  56.     UITouch * pTouch1 = [array objectAtIndex:0];  
  57.     UITouch * pTouch2 = [array objectAtIndex:1];  
  58.     //获取两个touch对象的坐标点  
  59.     CGPoint point1 = [pTouch1 locationInView:self];  
  60.     CGPoint point2 = [pTouch2 locationInView:self];  
  61.     //用封装的方法计算两触摸点之间的距离  
  62.     double distance = [self distanceOfPoint:point1 withPoint:point2];  
  63.     //判断两点间距离的变化  
  64.     if ((distance - _lastDistance) > 0)  
  65.     {  
  66.         //两点距离增大的方法,一般为图片、文字等的放大,捏合  
  67.         NSLog(@"两点距离变大");  
  68.     }  
  69.     else  
  70.     {  
  71.         //两点距离减小的方法,一般为图片、文字等的缩小,放开  
  72.         NSLog(@"两点距离变小");  
  73.     }  
  74.     //把现在的距离赋值给原来的距离,覆盖之  
  75.     _lastDistance = distance;  
  76. }  
  77. //编写一个计算两点之间距离的方法,封装此方法,方便调用  
  78. - (double)distanceOfPoint:(CGPoint)point1 withPoint:(CGPoint)point2  
  79. {  
  80.     double num1 = pow(point1.x - point2.x, 2);  
  81.     double num2 = pow(point1.y - point2.y, 2);  
  82.     double distance = sqrt(num1 + num2);  
  83.     return distance;  
  84. }  
  85. #pragma mark--------手离开屏幕,触摸事件结束  
  86. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event  
  87. {  
  88.     //触摸结束时发生的事件  
  89. }  
  90. #pragma mark--------触摸事件被打断,比如电话打进来  
  91. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event  
  92. {  
  93.     //一般不写此方法,可以把程序挂起,放在后台处理  
  94. }  
  95.   
  96. @end  




    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多