【问题标题】:When the user has two fingers on the screen and lifts one I want nothing to happen当用户在屏幕上有两根手指并抬起一根时,我不想发生任何事情
【发布时间】:2010-10-04 04:42:24
【问题描述】:

够了。

就像标题所说的,我有点吃醋了。

所以这是我的问题的概要。我有这艘船的权利。它位于屏幕的中心底部 (240,0)。当用户在大于 240 的点向右触摸时,船向右移动。如果用户在小于 240 的点上触摸,则离开。但是,当一次只有一根手指在屏幕上时,这很好。如果用户向右触摸,没有抬起手指,然后向左触摸(哦,废话,屏幕上有两个手指),我需要它到哪里,船一直向右移动,直到他们抬起右手,然后突然船向左移动第二次触摸。

我正在使用计时器从当前 x 坐标中添加/减去一个数字以使其移动。

简而言之,这就是我所拥有的:(不要笑或讨厌我是菜鸟)

-(void)moveShipLeft:(id)sender {

 shipView.center = CGPointMake(shipView.center.x - 2, 320 - (shipView.image.size.height / 2));

}
-(void)moveShipRight:(id)sender { 

 shipView.center = CGPointMake(shipView.center.x + 2, 320 - (shipView.image.size.height / 2));

}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

 UITouch *touch = [[event allTouches] anyObject];
 CGPoint location = [touch locationInView:touch.view];

 if (240 < location.x){rightTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(moveShipRight:) userInfo:nil repeats:YES];}
 if (240 > location.x){leftTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(moveShipLeft:) userInfo:nil repeats:YES];}

 [shipView setCenter:shipView.center];
 }
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {


  if (rightTimer != nil)               //Kill the timer that goes right upon touch removal
   [rightTimer invalidate];
  rightTimer = nil;

  if (leftTimer != nil)                //Kill the timer that goes left on touch removal
   [leftTimer invalidate];
  leftTimer = nil;
}

如果有帮助的话,我正在尝试完全复制游戏“Radiant”的控件。 如果你能帮忙,我会永远爱你。

澄清一下,我希望这种情况发生:

  1. 用户触摸屏幕右侧
  2. 船向右移动
  3. 用户用第二根手指触摸屏幕左侧
  4. 当船仍然向右移动时,因为用户没有解除他们的第一次触摸,然后用户解除了第一次触摸
  5. 然后船立即改变航向,向左移动到第二次触摸

现在我启用了多点触控,所以现在它的工作原理是这样的:

  1. 用户触摸屏幕右侧
  2. 船向右移动
  3. 用户用第二根手指触摸屏幕左侧
  4. 船向左移动,然后用户第一次触摸抬起
  5. 即使用户没有抬起第二次触摸,船也会停止前进

【问题讨论】:

    标签: iphone timer multi-touch touchesbegan touches


    【解决方案1】:

    首先,关于如果未释放 rightTouch 则不激活 leftTimer。

    您可以设置两个全局触摸。因此,如果 [touch locationInView:self.view] position blah > 250 globalRightTouch = touch。左边也一样。

    然后,当您击中右侧时-您将触摸分配给全局右侧,并且船开始向右移动-然后您同时触摸左侧-因此开始另一个触摸开始-因此您将其分配给globalLeftTouch。然后您可以执行以下操作: if(!globalRightTouch){start leftTimer) 反之亦然。

    在你的 touhcesEnded 函数中——如果在触摸结束时它不为空,你告诉 leftTimer 杀死——所以即使你的左触地仍然向下——你告诉两个计时器在任何触摸释放时都杀死。你只需要在你的触摸结束时更具体一点。

    然后在您的触摸结束代码中,您需要执行以下操作:

      UITouch *touch = [[event allTouches] anyObject];
    
      if (rightTimer != nil && touch == globalRightTouch){               //Kill the timer that goes right upon touch removal
       [rightTimer invalidate];
      rightTimer = nil;
       if(globalLeftTouch)
         leftTimerStartFunction //I dont know what your variables are sorry
      }
    
      if (leftTimer != nil && touch == globalLeftTouch){                //Kill the timer that goes left on touch removal
       [leftTimer invalidate];
      leftTimer = nil;
       if(globalRightTouch){
          rightTimerStartFunction //I dont know what you variables are sorry
       }
     }
    }
    

    所以本质上你想说 - 如果我放开触摸 - 检查它是哪个触摸并终止该触摸的计时器,如果它不为空并且屏幕上当前有另一个触摸 - 启动计时器那边。

    希望这会有所帮助,

    干杯,

    迈克尔

    【讨论】:

    • 谢谢,我很确定这会奏效。我只是在脑海中解决了这个问题,看起来还不错。所以我到底会在我的触摸开始。我想我有一个想法,但它没有按照我想要的方式工作。非常感谢您的帮助我非常感谢它^_^
    • 我想如果它能让飞船更容易一点,在用户抬起他们的第一次触摸之前,飞船就不必进行第二次触摸
    • 等一下,我仍然需要您的帮助来构建我所接触的内容。把你说的改写好一点
    • 所以我得到了这个:- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint locationmoved = [touch locationInView:touch.view]; if(locationmoved.x 240){ globalLeftTouch = touch; } if(!globalRightTouch){//rightTimer} if(!globalLeftTouch){//leftTimer} [shipView setCenter:shipView.center];} 但它不起作用。在模拟器中,它仅适用于第一次触摸,之后的任何触摸船都不会让步
    • 大声将代码复制并粘贴到 word 中,抱歉它没有正确显示
    【解决方案2】:

    从用户开始触摸到停止,每个 UITouch 都是独一无二的。所以你的问题真的是这一行:

     UITouch *touch = [[event allTouches] anyObject];
    

    您不希望只是 任何 触摸。你想要一种与你一直在努力的触摸相同的触摸,如果它存在的话。在集合中寻找那个触球,如果没有,就开始转向另一个触球。

    【讨论】:

      【解决方案3】:

      这是一个真实/伪代码版本 - 希望对您有所帮助...

      -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)even{
      UITouch *touch = [touches anyObject];
      
      if(touch.x < 240){
          globalTouchLeft = touch;
          if(!globalTouchRight){
              startLeftTimer; //again I don't know what your timere/variable is
          }
      }else if(touch.x  >= 240){
      
          globalTouchRight = touch;
          if(!globalTouchLeft){
              startRightTimer; //again I don't know what your timere/variable is
          }
      
          }
      }
      
      -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
      
          UITouch *touch = [touches anyObject];
      
          if(touch == globalLeftTouch){
              stopLeftTimer
              globalLeftTouch = nil;
      
              if(globalRightTouch){
                  startRightTimer;
              }
      
          }
      
          if(touch == globalTouchRight){
              stopRightTimer
              globalRightTouch = nil;
      
              if(globalLeftTouch){
                  startLeftTimer;
              }
          }
      }
      

      干杯,

      迈克尔·奥布莱恩

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-28
        相关资源
        最近更新 更多