【问题标题】:Detect when UIGestureRecognizer is up, down, left and right Cocos2d检测UIGestureRecognizer何时上下左右Cocos2d
【发布时间】:2011-11-17 05:13:26
【问题描述】:

我有一个 CCSprite,我想用手势四处移动。问题是我对 Cocos2D 完全陌生。我希望我的精灵在手势向上时执行一个动作,在手势向下时执行另一个动作,在手势正确时执行另一个动作,向左执行相同操作。有人可以指出我正确的方向吗?

谢谢!

【问题讨论】:

    标签: iphone xcode cocos2d-iphone uigesturerecognizer


    【解决方案1】:

    尽管这里有很多很好的信息,但我找不到一个包含所有信息的快速答案。

    如果您想区分滑动是leftrightupdown,您需要为每个方向创建一个 UISwipeGestureRecognizer .

    但是!这还不错,因为您可以将每个手势识别器路由到同一个选择器,然后可以使用您可能期望的 switch 语句。

    首先,为每个方向添加手势识别器并将它们路由到同一个选择器:

    - (void)setupSwipeGestureRecognizers
    {
        UISwipeGestureRecognizer *rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(userDidSwipeScreen:)];
        rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
        UISwipeGestureRecognizer *leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(userDidSwipeScreen:)];
        leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
        [self.view addGestureRecognizer:rightSwipeGestureRecognizer];
        [self.view addGestureRecognizer:leftSwipeGestureRecognizer];
    }
    

    第二,用switch语句区分方向:

    - (void)userDidSwipeScreen:(UISwipeGestureRecognizer *)swipeGestureRecognizer
    {
        switch (swipeGestureRecognizer.direction) {
            case UISwipeGestureRecognizerDirectionLeft: {
                // Handle left
                break;
            }
            case UISwipeGestureRecognizerDirectionRight: {
                // Handle right
                break;
            }
            default: {
                break;
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      为每个斧头(水平和垂直)添加一个 UISwipeGestureRecognizer:

      UISwipeGestureRecognizer *horizontalSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(action)];
      [horizontalSwipe setDirection:(UISwipeGestureRecognizerDirectionRight |
                                 UISwipeGestureRecognizerDirectionLeft )];
      
      [self.view addGestureRecognizer:horizontalSwipe];
      
      UISwipeGestureRecognizer *verticalSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(action)];
      [verticalSwipe setDirection:(UISwipeGestureRecognizerDirectionUp |
                           UISwipeGestureRecognizerDirectionDown )];
      
      [self.view addGestureRecognizer:verticalSwipe];
      

      【讨论】:

        【解决方案3】:

        默认方向为 UISwipeGestureRecognizerDirectionRight。多个方向也可以这样指定:

        [swipeGesture setDirection: UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionLeft];
        

        /// 但是如果你想得到每一个方向,就像这样:

         UISwipeGestureRecognizer *swipeGestureR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGestureRight:)];
        [swipeGestureR setDirection: UISwipeGestureRecognizerDirectionRight ];
         [[[CCDirector sharedDirector] openGLView] addGestureRecognizer:swipeGestureR];
        
        [swipeGestureR release];
        
        UISwipeGestureRecognizer *swipeGestureL = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGestureLeft:)];
        [swipeGestureL setDirection: UISwipeGestureRecognizerDirectionLeft];
        [[[CCDirector sharedDirector] openGLView] addGestureRecognizer:swipeGestureL];
        
        [swipeGestureL release];
        

        向左滑动调用handleSwipeGestureLeft函数,向右滑动调用handleSwipeGestureRight函数

        【讨论】:

          【解决方案4】:

          使用 UIPanGestureRecogizer 并检测您关心的滑动方向。有关详细信息,请参阅 UIPanGestureRecognizer 文档。 -rrh

          // add pan recognizer to the view when initialized
          UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panRecognized:)];
          [panRecognizer setDelegate:self];
          [self addGestureRecognizer:panRecognizer]; // add to the view you want to detect swipe on
          
          
          -(void)panRecognized:(UIPanGestureRecognizer *)sender
          {
              if (sender.state == UIGestureRecognizerStateBegan) {
                  // you might want to do something at the start of the pan
              }
          
              CGPoint distance = [sender translationInView:self]; // get distance of pan/swipe in the view in which the gesture recognizer was added
              CGPoint velocity = [sender velocityInView:self]; // get velocity of pan/swipe in the view in which the gesture recognizer was added
              float usersSwipeSpeed = abs(velocity.x); // use this if you need to move an object at a speed that matches the users swipe speed
              NSLog(@"swipe speed:%f", usersSwipeSpeed);
              if (sender.state == UIGestureRecognizerStateEnded) {
                  [sender cancelsTouchesInView]; // you may or may not need this - check documentation if unsure
                  if (distance.x > 0) { // right
                      NSLog(@"user swiped right");
                  } else if (distance.x < 0) { //left
                      NSLog(@"user swiped left");
                  }
                  if (distance.y > 0) { // down
                      NSLog(@"user swiped down");
                  } else if (distance.y < 0) { //up
                      NSLog(@"user swiped up");
                  }
                  // Note: if you don't want both axis directions to be triggered (i.e. up and right) you can add a tolerence instead of checking the distance against 0 you could check for greater and less than 50 or 100, etc.
              }
          }
          

          【讨论】:

          • 有趣的解决方案,但值得一提的是UIGestureRecognizer 及其子类的很大一部分是标准化手势的实现方式......您的代码包含并建议使用您自己的逻辑区分不同方向、速度等的滑动,这意味着您可能会面临与人们在 iOS 中习惯的“感觉不同”的手势实现的风险。也就是说,在某些情况下,这可能是一个好方法,例如当您想让滑动/平移的对象在滑动时移动。
          【解决方案5】:
          UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
          swipeGesture.direction = UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionDown;
          [self.gestureAreaView addGestureRecognizer:swipeGesture];
          [swipeGesture release];
          
          -(void)handleSwipeGesture:(UISwipeGestureRecognizer *) sender 
          {
              //Gesture detect - swipe up/down , can't be recognized direction
          }
          
          or
          
          UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
          swipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
          [self.view addGestureRecognizer:swipeGesture];
          [swipeGesture release];
          
          UISwipeGestureRecognizer *swipeGesture2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
          swipeGesture2.direction = UISwipeGestureRecognizerDirectionDown;
          [self.view addGestureRecognizer:swipeGesture2];
          [swipeGesture2 release];
          
          -(void)handleSwipeGesture:(UISwipeGestureRecognizer *) sender 
          {
              //Gesture detect - swipe up/down , can be recognized direction
              if(sender.direction == UISwipeGestureRecognizerDirectionUp)
              {
              }
              else if(sender.direction == UISwipeGestureRecognizerDirectionDown)
              {
              }
          }
          

          【讨论】:

          • 感谢您发布答案!虽然代码 sn-p 可以回答这个问题,但添加一些附加信息仍然很棒,比如解释等..
          【解决方案6】:

          显然每个 UISwipeGestureRecognizer 只能检测到给定方向的滑动。即使方向标志可以被或在一起,UISwipeGestureRecognizer 也会忽略其他标志。

          解决方案是为您希望识别滑动手势的每个方向添加一个 UISwipeGestureRecognizer,并将每个识别器的方向相应地设置为上、下、左和右。如果你想测试任何方向的滑动,你必须添加四个 UISwipeGestureRecognizers。

          这有点奇怪,但这是对我有用的唯一方法。

          【讨论】:

          【解决方案7】:
          -(void)addGesture {
          UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
              [self.view addGestureRecognizer:swipeGesture];
              [swipeGesture release];
          }
          
          -(void)handleSwipeGesture:(UISwipeGestureRecognizer *) sender {
          
          if (sender.direction == UISwipeGestureRecognizerDirectionUp) {
            //do something
           }
          else if (sender.direction == UISwipeGestureRecognizerDirectionDown) {
            //do something
           }
          else if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
            //do something
           }
          else if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
            //do something
           }
          
          
          }
          

          也可以使用 switch 代替所有 if 语句

          【讨论】:

          • 我添加了这个,但它只检测方向是否正确。我添加了这样的手势: -(void)addGesture { UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)]; [[[CCDirector sharedDirector] openGLView] addGestureRecognizer:swipeGesture]; [swipeGesture释放]; }
          • Put NSLog("手势方向:%@", sender.direction);在 -(void)handleSwipeGesture 方法的开头,看看当你向各个方向滑动时会得到什么......奇怪的是它只识别正确。
          • 如果我 NSLog 它,输出是: 1 这是正确的值。有没有其他方法可以检测 cocos2d 中的手势?
          • gesture.direction 默认是正确的,这就是为什么你只得到正确的 (1)。
          猜你喜欢
          • 2010-11-14
          • 2012-10-17
          • 1970-01-01
          • 1970-01-01
          • 2013-09-02
          • 2018-12-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多