UITouch,主要是重写四个方法(触摸开始、触摸移动、触摸结束、触摸退出)以实现触摸响应方法
、触摸开始
、触摸移动
、触摸结束
、触摸退出(注意:通常是受到设备其他方法的影响:如来电、电量不足关机等)
使用注意事项:
1、触摸开始方法中,一般实现的属性有:
1-1、获取触摸对象,如:
[objc] view plain copy
1. UITouch *touch = touches.anyObject;
1-2、触摸次数,以便判断实现对应方法,如:
[objc] view plain copy
1. NSInteger clickNumber = touch.tapCount;
2. NSLog(@"click number = %@", @(clickNumber));
1-3、触摸位置是否用户想要处理的子视图,如:
[objc] view plain copy
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
1. CGPoint currentPoint = [touch locationInView:self.view];
2. CGPoint previousPoint = [touch previousLocationInView:self.view];
注:可通过触摸移动改变子视图的位置,如:
[objc] view plain copy
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
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. @"触摸手势 开始");
7.
8.
9. // 获取触摸对象
10. UITouch *touch = touches.anyObject;
11. // 获取触摸次数
12. .tapCount;
13. @"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. .phase;
24. @"state = %ld", state);
25. // 获取响应视图对象
26. UIView *view = touch.view;
27. @"view = %@", view);
28. // 获取当前触摸位置
29. locationInView:self.view];
30. @"touch.locationInView = {%2.3f, %2.3f}", currentPoint.x, currentPoint.y);
31. // 获取当前触摸的前一个位置
32. previousLocationInView:self.view];
33. @"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
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. @"触摸手势 移动");
7.
8. // 移动UI视图控件
9. // 方法1 获得触摸点的集合,可以判断多点触摸事件
10. for (UITouch *touch in event.allTouches)
11. {
12. // 获取当前触摸坐标
13. locationInView:self.view];
14. @"touch.locationInView = {%2.3f, %2.3f}", currentPoint.x, currentPoint.y);
15.
16. // 获取当前触摸前一个坐标
17. previousLocationInView:self.view];
18. @"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. self.label.center.x + moveX;
24. 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
1. // 触摸结束
2. - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
3. {
4. // self.view.backgroundColor = [UIColor whiteColor];
5.
6. @"触摸手势 结束");
7.
8. self.label.text = @"touch触摸手势";
9. }
[objc] view plain copy
1. // 触摸退出(注意:通常是受到设备其他方法的影响:如来电、电量不足关机等)
2. - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
3. {
4. @"触摸手势 退出");
5. }
效果图