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、获取触摸对象,如:
- UITouch *touch = touches.anyObject;
1-2、触摸次数,以便判断实现对应方法,如:
- NSInteger clickNumber = touch.tapCount;
- NSLog(@"click number = %@", @(clickNumber));
1-3、触摸位置是否用户想要处理的子视图,如:
- CGPoint currentPoint = [touch locationInView:self.view];
- if (CGRectContainsPoint(self.label.frame, currentPoint))
- {
-
- self.label.text = @"触摸位置在当前label";
- }
2、触摸移动方法中,一般实现的属性有:
2-1、获取触摸对象,如:
UITouch *touch = touches.anyObject;
2-2、获取当前点击位置,以及上一个点击位置,如:
- CGPoint currentPoint = [touch locationInView:self.view];
- CGPoint previousPoint = [touch previousLocationInView:self.view];
注:可通过触摸移动改变子视图的位置,如:
- float moveX = currentPoint.x - previousPoint.x;
- float moveY = currentPoint.y - previousPoint.y;
- CGFloat centerX = self.label.center.x + moveX;
- CGFloat centerY = self.label.center.y + moveY;
- self.label.center = CGPointMake(centerX, centerY);
-
- - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
- {
-
-
- NSLog(@"触摸手势 开始");
-
-
-
- UITouch *touch = touches.anyObject;
-
- NSInteger clickNumber = touch.tapCount;
- NSLog(@"click number = %@", @(clickNumber));
- if (2 == clickNumber)
- {
- self.label.backgroundColor = [UIColor redColor];
- }
- else if (3 == clickNumber)
- {
- self.label.backgroundColor = [UIColor greenColor];
- }
-
- UITouchPhase state = touch.phase;
- NSLog(@"state = %ld", state);
-
- UIView *view = touch.view;
- NSLog(@"view = %@", view);
-
- CGPoint currentPoint = [touch locationInView:self.view];
- NSLog(@"touch.locationInView = {%2.3f, %2.3f}", currentPoint.x, currentPoint.y);
-
- CGPoint previousPoint = [touch previousLocationInView:self.view];
- NSLog(@"touch.previousLocationInView = {%2.3f, %2.3f}", previousPoint.x, previousPoint.y);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if (CGRectContainsPoint(self.label.frame, currentPoint))
- {
-
- self.label.text = @"触摸位置在当前label";
- }
- }
-
- - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
- {
-
-
- NSLog(@"触摸手势 移动");
-
-
-
- for (UITouch *touch in event.allTouches)
- {
-
- CGPoint currentPoint = [touch locationInView:self.view];
- NSLog(@"touch.locationInView = {%2.3f, %2.3f}", currentPoint.x, currentPoint.y);
-
-
- CGPoint previousPoint = [touch previousLocationInView:self.view];
- NSLog(@"touch.previousLocationInView = {%2.3f, %2.3f}", previousPoint.x, previousPoint.y);
-
-
- float moveX = currentPoint.x - previousPoint.x;
- float moveY = currentPoint.y - previousPoint.y;
- CGFloat centerX = self.label.center.x + moveX;
- CGFloat centerY = self.label.center.y + moveY;
- self.label.center = CGPointMake(centerX, centerY);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
-
- - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
- {
-
-
- NSLog(@"触摸手势 结束");
-
- self.label.text = @"touch触摸手势";
- }
-
- - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
- {
- NSLog(@"触摸手势 退出");
- }
效果图