iOS触摸手势——UITouch

来源:互联网 发布:js执行exe文件 编辑:程序博客网 时间:2024/05/16 00:44

Demo实例


UITouch,主要是重写四个方法(触摸开始、触摸移动、触摸结束、触摸退出)以实现触摸响应方法

 1、触摸开始 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { }

 2、触摸移动 - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { }

 3、触摸结束 - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { }

 4、触摸退出(注意:通常是受到设备其他方法的影响:如来电、电量不足关机等) - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { }

 

 使用注意事项:

 1、触摸开始方法中,一般实现的属性有:

 1-1、获取触摸对象,如:

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. UITouch *touch = touches.anyObject;  

 1-2、触摸次数,以便判断实现对应方法,如:

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. NSInteger clickNumber = touch.tapCount;  
  2. NSLog(@"click number = %@", @(clickNumber));  

 1-3、触摸位置是否用户想要处理的子视图,如:

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. CGPoint currentPoint = [touch locationInView:self.view];  
  2. if (CGRectContainsPoint(self.label.frame, currentPoint))  
  3. {  
  4.    // 改变标题  
  5.    self.label.text = @"触摸位置在当前label";  
  6. }  

 2、触摸移动方法中,一般实现的属性有:

 2-1、获取触摸对象,如:

 UITouch *touch = touches.anyObject;

 2-2、获取当前点击位置,以及上一个点击位置,如:

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. CGPoint currentPoint = [touch locationInView:self.view];  
  2. CGPoint previousPoint = [touch previousLocationInView:self.view];  

 注:可通过触摸移动改变子视图的位置,如:

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. float moveX = currentPoint.x - previousPoint.x;  
  2. float moveY = currentPoint.y - previousPoint.y;  
  3. CGFloat centerX = self.label.center.x + moveX;  
  4. CGFloat centerY = self.label.center.y + moveY;  
  5. self.label.center = CGPointMake(centerX, centerY);  

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 触摸开始  
  2. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event  
  3. {  
  4. //    self.view.backgroundColor = [UIColor colorWithRed:(arc4random() % 255 / 255.0) green:(arc4random() % 255 / 255.0) blue:(arc4random() % 255 / 255.0) alpha:1.0];  
  5.       
  6.     NSLog(@"触摸手势 开始");  
  7.       
  8.       
  9.     // 获取触摸对象  
  10.     UITouch *touch = touches.anyObject;  
  11.     // 获取触摸次数  
  12.     NSInteger clickNumber = touch.tapCount;  
  13.     NSLog(@"click number = %@", @(clickNumber));  
  14.     if (2 == clickNumber)  
  15.     {  
  16.         self.label.backgroundColor = [UIColor redColor];  
  17.     }  
  18.     else if (3 == clickNumber)  
  19.     {  
  20.         self.label.backgroundColor = [UIColor greenColor];  
  21.     }  
  22.     // 获取触摸状态  
  23.     UITouchPhase state = touch.phase;  
  24.     NSLog(@"state = %ld", state);  
  25.     // 获取响应视图对象  
  26.     UIView *view = touch.view;  
  27.     NSLog(@"view = %@", view);  
  28.     // 获取当前触摸位置  
  29.     CGPoint currentPoint = [touch locationInView:self.view];  
  30.     NSLog(@"touch.locationInView = {%2.3f, %2.3f}", currentPoint.x, currentPoint.y);  
  31.     // 获取当前触摸的前一个位置  
  32.     CGPoint previousPoint = [touch previousLocationInView:self.view];  
  33.     NSLog(@"touch.previousLocationInView = {%2.3f, %2.3f}", previousPoint.x, previousPoint.y);  
  34.     // 获取触摸区域的子视图  
  35.     // 方法1  
  36. //    for (UIView *oneView in self.view.subviews)  
  37. //    {  
  38. //        if ([oneView isKindOfClass:[UILabel class]])  
  39. //        {  
  40. //            // 是否选中图标(当前坐标是否包含所选图标)  
  41. //            if (CGRectContainsPoint(oneView.frame, currentPoint))  
  42. //            {  
  43. //                // 获取当前被选择图标  
  44. //                UILabel *selectedView = (UILabel *)oneView;  
  45. //                // 改变标题  
  46. //                selectedView.text = @"触摸位置在当前label";  
  47. //            }  
  48. //        }  
  49. //    }  
  50.     // 方法2  
  51.     if (CGRectContainsPoint(self.label.frame, currentPoint))  
  52.     {  
  53.         // 改变标题  
  54.         self.label.text = @"触摸位置在当前label";  
  55.     }  
  56. }  

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 触摸移动  
  2. - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event  
  3. {  
  4. //    self.view.backgroundColor = [UIColor colorWithRed:(arc4random() % 255 / 255.0) green:(arc4random() % 255 / 255.0) blue:(arc4random() % 255 / 255.0) alpha:1.0];  
  5.       
  6.     NSLog(@"触摸手势 移动");  
  7.       
  8.     // 移动UI视图控件  
  9.     // 方法1 获得触摸点的集合,可以判断多点触摸事件  
  10.     for (UITouch *touch in event.allTouches)  
  11.     {  
  12.         // 获取当前触摸坐标  
  13.         CGPoint currentPoint = [touch locationInView:self.view];  
  14.         NSLog(@"touch.locationInView = {%2.3f, %2.3f}", currentPoint.x, currentPoint.y);  
  15.           
  16.         // 获取当前触摸前一个坐标  
  17.         CGPoint previousPoint = [touch previousLocationInView:self.view];  
  18.         NSLog(@"touch.previousLocationInView = {%2.3f, %2.3f}", previousPoint.x, previousPoint.y);  
  19.           
  20.         // 坐标偏移量 (效果异常??)  
  21.         float moveX = currentPoint.x - previousPoint.x;  
  22.         float moveY = currentPoint.y - previousPoint.y;  
  23.         CGFloat centerX = self.label.center.x + moveX;  
  24.         CGFloat centerY = self.label.center.y + moveY;  
  25.         self.label.center = CGPointMake(centerX, centerY);  
  26.     }  
  27.     // 方法2  
  28. //    UITouch *touch = touches.anyObject;  
  29. //    // 获取当前触摸坐标  
  30. //    CGPoint currentPoint = [touch locationInView:self.view];  
  31. //    NSLog(@"touch.locationInView = {%2.3f, %2.3f}", currentPoint.x, currentPoint.y);  
  32. //    // 获取当前触摸前一个坐标  
  33. //    CGPoint previousPoint = [touch previousLocationInView:self.view];  
  34. //    NSLog(@"touch.previousLocationInView = {%2.3f, %2.3f}", previousPoint.x, previousPoint.y);  
  35. //    // 坐标偏移量  
  36. //    float moveX = currentPoint.x - previousPoint.x;  
  37. //    float moveY = currentPoint.y - previousPoint.y;  
  38. //    CGFloat centerX = self.label.center.x + moveX;  
  39. //    CGFloat centerY = self.label.center.y + moveY;  
  40. //    self.label.center = CGPointMake(centerX, centerY);  
  41. }  

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 触摸结束  
  2. - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event  
  3. {  
  4. //    self.view.backgroundColor = [UIColor whiteColor];  
  5.       
  6.     NSLog(@"触摸手势 结束");  
  7.       
  8.     self.label.text = @"touch触摸手势";  
  9. }  

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 触摸退出(注意:通常是受到设备其他方法的影响:如来电、电量不足关机等)  
  2. - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event  
  3. {  
  4.     NSLog(@"触摸手势 退出");  
  5. }  

效果图



0 0
原创粉丝点击