不同 uiview 中的 iOS 多点触控事件

标签 ios objective-c iphone uiview

我在 UIViewcontroller 上有两个不同的 UIView。

那个UIView就是左UIView(leftScreenView)和右UIView(rightScreenView)。

它们是区分 subview 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;

我的 Storyboard截图:

enter image description here

非常感谢。

最佳答案

UIGestureRecognizer 添加到 -viewDidLoad 中的每个 View 。使用 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 中,您可以跟踪包含手势的状态和 View 。

- (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){

    }

}

编辑:

要区分左 View 和右 View ,您可以为每个 View 添加一个标签

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

然后在-handlePan:

里面
UIView *view = gesture.view; //View that contains gesture

if(view.tag == 0)
   //...

if(view.tag == 1)
   //...

编辑2:

您需要将手势添加到左右 View ,而不是 View Controller 的 View 。

- (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];

}

关于不同 uiview 中的 iOS 多点触控事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30117145/

相关文章:

iPhone - 设置 UIImage 位置

iphone - 如何以编程方式使铃声静音或更改 iOS5 上的铃声

ios - 在 SWIFT 中收到 "<null>"作为来自 api 的响应时崩溃

ios - 从 iOS 框架中移除代码设计

iphone - 开始或停止输入字符时 UITextField 字体大小发生变化

ios - Unity for iOS 在后台占用大量内存

ios - 如何为 iOS 应用程序创建带有动画的自定义 View 或控件?

ios - Objective-C - 禁用特定的调试屏幕

iphone - 我可以通过 UIAppearance 代理设置哪些属性?

iPhone 开发者 : Swipe a UITableCell