Detect when UIGestureRecognizer is up, down, left and right Cocos2d

前端 未结 7 1448
闹比i
闹比i 2020-12-01 09:28

I have a CCSprite that I want to move around using gestures. Problem is I\'m completely new to Cocos2D. I want my sprite to perform one action when the gesture is up, anothe

7条回答
  •  半阙折子戏
    2020-12-01 09:50

    -(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
     }
    
    
    }
    

    Can also use a switch instead of all the if statements

提交回复
热议问题