不同uiview中的iOS MultiTouch事件
Posted
技术标签:
【中文标题】不同uiview中的iOS MultiTouch事件【英文标题】:iOS MultiTouch Event in different uiview 【发布时间】:2015-05-08 06:28:00 【问题描述】:我在 UIViewcontroller 上有两个不同的 UIView。
那个UIView是左UIView(leftScreenView)和右UIView(rightScreenView)。
它们是区分子视图 leftJoystickView(on leftScreenView) 和 rightJoystickView(on rightScreenView)。
但我发现以下问题:
当我在 leftScreenView 上触摸并移动时,现在我在 rightScreenView 上触摸其他手指。
现在触摸事件总是在 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;
我的故事板截图:
非常感谢。
【问题讨论】:
如果要区分开始和结束,为什么不用UIGestureRecognizer
?
因为在开始时,移动和结束在操纵杆上的动作不同。所以我使用了touchbegan。如果使用 UIGestureRecognizer 如何区分不同视图上的触摸开始?谢谢。
您可以查看UIGestureRecognizer
的状态。
请问,如果使用 UIGestureRecognizer,addGestureRecognizer:tapGesture 是添加到 self.view 还是 self.leftScreenView 和 self.rightScreenView?谢谢。
您将其添加到特定视图中。所以:self.view.addGestureRecognizer:tapGesture
或 self.leftScreenView.addGestureRecognizer:tapGesture
【参考方案1】:
在-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:
里面
UIView *view = gesture.view; //View that contains gesture
if(view.tag == 0)
//...
if(view.tag == 1)
//...
编辑2:
你需要将手势添加到左右视图,而不是视图控制器的视图。
- (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];
【讨论】:
如果使用UIPanGestureRecognizer,如何区分leftScreenView和right ScrrenView应该调用[self.leftJoystickView touchesMoved:touches withEvent:event]; , 请。谢谢 对不起,我试试你的代码,如果我拖到 tag=0(leftVIew), not touch up(end) 的情况。然后点击右视图,没有显示日志标签=1(右视图)。@@ 首先,谢谢。如果我想触摸开始和触摸结束事件,我应该 UITapGestureRecognizer 对吗? UIPanGestureRecognizer 仅适用于移动情况。 @dickfala 是的,请参阅此处的文档 - developer.apple.com/library/ios/documentation/UIKit/Reference/… 并请接受我的回答) 等我解决我的问题,然后使用您的答案申请我的申请。我会接受你的回答。首先谢谢你。【参考方案2】:您可以使用- touchesForView:
或UIEvent
处理这种情况。 - touchesForView:
返回 UITouch
集合。检查返回的集合是否包含leftSideView或RightSideView,如果是则处理移动事件。以下是代码sn-p:
-(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
来实现。
【讨论】:
以上是关于不同uiview中的iOS MultiTouch事件的主要内容,如果未能解决你的问题,请参考以下文章
iOS,相同尺寸的UIView在不同的iphone上显示不同的尺寸?
如何在 iOS Swift 中将不同的角半径应用于 UIView 的不同角?