我在UIViewcontroller上有两个不同的UIView。
UIView是UIView(leftScreenView)和右UIView(rightScreenView)。
它们区分子视图leftJoystickView(在leftScreenView上)和rightJoystickView(在rightScreenView上)。
但我发现下面的问题:
当我触摸leftScreenView并移动时,现在我触摸右侧屏幕上的其他手指。
现在触摸事件总是在leftScreenView上。这不能区分leftScreenView和rightScreenView事件。
我需要触摸leftScreenView并移动并触摸rightScreenView移动是同一时间的不同事件(做移动)。
如何处理区分不同移动事件和开始事件的多点触控?
#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
如何在不同的uiview上处理多点触控?
我在viewdidload中添加了以下内容:
self.leftScreenView.multipleTouchEnabled = NO;
self.rightScreenView.multipleTouchEnabled = NO;
// self.leftScreenView.exclusiveTouch = NO;
// self.rightScreenView.exclusiveTouch = NO;
self.view.multipleTouchEnabled = YES;
我的故事板截图:
非常感谢。
答案 0 :(得分:2)
在UIGestureRecognizer
内的每个观看次数中添加-viewDidLoad
。使用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:
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];
}
答案 1 :(得分:1)
您可以使用- touchesForView:
的{{1}}处理此情况。 UIEvent
会返回- touchesForView:
集。检查返回的集合是否包含leftSideView或RightSideView,如果是,则处理move事件。以下是代码段:
UITouch
}
这也可以通过使用-(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
}
}
来实现。