在不同的uiview中的iOS MultiTouch事件

UIViewcontroller上有两个不同的UIView。

该UIView是留下UIView(leftScreenView)和右UIView(rightScreenView)。

他们区分子视图leftJoystickView(在leftScreenView)和rightJoystickView(在rightScreenView)。

但是我发现下面的问题:

当我触摸leftScreenView并移动时,现在我触摸右边的ScreenView上的其他手指。

现在触摸事件总是在leftScreenView上。 这不能区分leftScreenView和rightScreenView事件。

我需要触摸左边的ScreenView,并且移动和触摸右边的ScreenView移动是不同的事件(做移动)在同一时间。

如何处理多点触摸区分不同的移动事件并开始事件?

#pragma mark ----- touch action ----- -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // NSLog(@"------touchesBegan-------"); UITouch *touch = [[event allTouches] anyObject]; CGPoint touchViewPoint = [touch locationInView:touch.view]; CGPoint touchLeftScreenPoint = [touch.view convertPoint:touchViewPoint toView:self.leftScreenView]; CGPoint touchRightScreenPoint = [touch.view convertPoint:touchViewPoint toView:self.rightScreenView]; NSLog(@"touch left:%d",[self.leftScreenView pointInside:touchLeftScreenPoint withEvent:event]); NSLog(@"touch right:%d",[self.rightScreenView pointInside:touchRightScreenPoint withEvent:event]); NSLog(@"touch.tapCount:%ld", touch.tapCount); if( [self.leftScreenView pointInside:touchLeftScreenPoint withEvent:event] ) { NSLog(@"began click left screen"); self.leftStickLfConstraint.constant = touchLeftScreenPoint.x ; self.leftStickTopStickConstraint.constant = touchLeftScreenPoint.y ; [self.leftJoystickView touchesBegan:touches withEvent:event]; }else if( [self.rightScreenView pointInside:touchRightScreenPoint withEvent:event] ) { NSLog(@"began click right screen"); self.rightStickLfConstraint.constant = touchRightScreenPoint.x ; self.rightStickTopConstraint.constant = touchRightScreenPoint.y ; [self.rightJoystickView touchesBegan:touches withEvent:event]; } NSLog(@" "); } 

移动事件如下:

  -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint touchViewPoint = [touch locationInView:touch.view]; NSLog(@"moved touch.tapCount:%ld", touch.tapCount); NSLog(@"moved touches count:%ld", [touches count]); CGPoint touchLeftScreenPoint = [touch.view convertPoint:touchViewPoint toView:self.leftScreenView]; CGPoint touchRightScreenPoint = [touch.view convertPoint:touchViewPoint toView:self.rightScreenView]; if( [self.leftScreenView pointInside:touchLeftScreenPoint withEvent:event] ) { NSLog(@"touchesMoved click left screen"); [self.leftJoystickView touchesMoved:touches withEvent:event]; }else if( [self.rightScreenView pointInside:touchRightScreenPoint withEvent:event] ) { NSLog(@"touchesMoved click right screen"); [self.rightJoystickView touchesMoved:touches withEvent:event]; } } 

当我继续移动leftScreenView,然后触摸rightScreenView。

我总是碰到左边:1碰右边:0。

日志:

  2015-05-08 14:20:30.946 DroneG2[3606:942222] touchesMoved click left screen 2015-05-08 14:20:30.947 DroneG2[3606:942222] touches count:1 2015-05-08 14:20:30.962 DroneG2[3606:942222] moved touch.tapCount:1 2015-05-08 14:20:30.962 DroneG2[3606:942222] moved touches count:1 2015-05-08 14:20:30.962 DroneG2[3606:942222] touchesMoved click left screen 2015-05-08 14:20:30.963 DroneG2[3606:942222] touches count:1 2015-05-08 14:20:30.982 DroneG2[3606:942222] moved touch.tapCount:1 2015-05-08 14:20:30.982 DroneG2[3606:942222] moved touches count:1 2015-05-08 14:20:30.983 DroneG2[3606:942222] touchesMoved click left screen 2015-05-08 14:20:30.983 DroneG2[3606:942222] touches count:1 2015-05-08 14:20:30.984 DroneG2[3606:942222] touch left:1 2015-05-08 14:20:30.985 DroneG2[3606:942222] touch right:0 

我如何处理不同的uview上的多点触摸?

我在viewdidload中添加了下面的代码:

  self.leftScreenView.multipleTouchEnabled = NO; self.rightScreenView.multipleTouchEnabled = NO; // self.leftScreenView.exclusiveTouch = NO; // self.rightScreenView.exclusiveTouch = NO; self.view.multipleTouchEnabled = YES; 

我的故事板截图:

在这里输入图像说明

非常感谢你。

-viewDidLoad里面添加一个UIGestureRecognizer到你的每个视图。 使用UIGestureRecognizer您可以跟踪手势的状态。 作为一个例子,你可以使用以下内容。

 //Add a gesture recognizer to each view UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action: @selector(handlePan:)]; panGesture.minimumNumberOfTouches = 1; [self.myView addGestureRecognizer:panGesture]; 

现在在-handlePan可以跟踪包含手势的状态和视图。

 - (void)handlePan:(UIPanGestureRecognizer *)gesture{ UIView *view = gesture.view; //View that contains gesture if(gesture.state == UIGestureRecognizerStateBegan){ }else if(gesture.state == UIGestureRecognizerStateChanged){ }else if(gesture.state == UIGestureRecognizerStateEnded){ } } 

编辑:

要区分左侧视图和右侧视图,可以为每个视图添加一个tag

 leftView.tag = 0; rightView.tag = 1; 

然后在-handlePan:里面-handlePan:

 UIView *view = gesture.view; //View that contains gesture if(view.tag == 0) //... if(view.tag == 1) //... 

EDIT2:

您需要将手势添加到左侧和右侧视图,而不是视图控制器的视图。

 - (void)viewDidLoad { [super viewDidLoad]; self.leftScreenView.tag = 0; self.rightScreenView.tag = 1; UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action: @selector(handlePan:)]; panGesture.minimumNumberOfTouches = 1; [self.leftScreenView addGestureRecognizer:panGesture]; UIPanGestureRecognizer *panGesture1 = [[UIPanGestureRecognizer alloc] initWithTarget:self action: @selector(handlePan:)]; panGesture1.minimumNumberOfTouches = 1; [self.rightScreenView addGestureRecognizer:panGesture1]; } 

你可以使用- touchesForView:UIEvent来处理这种情况。 - touchesForView:返回UITouch集合。 检查返回的集合是否包含leftSideView或RightSideView,如果是,则处理移动事件。 以下是代码片段:

 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ // get the touch set of leftside view NSSet *eventTouches1 = [event touchesForView:leftSideView]; // get the touch set of rightside view NSSet *eventTouches2 = [event touchesForView:rightSideView]; // check if the eventTouches1 is not null if (eventTouches1) { UITouch *touch1 = [[eventTouches1 allObjects] objectAtIndex:0]; if ([touch1.view isEqual:leftSideView]) { // handle drag event for left side view } } // check if the eventTouches2 is not null if (eventTouches2) { UITouch *touch2 = [[eventTouches2 allObjects] objectAtIndex:0]; if ([touch2.view isEqual:rightSideView]) { // handle drag event for right side view } } 

}

这也可以通过使用UIGestureRecognizer来实现。