webdevqa.jp.net

UIGestureRecognizerが上、下、左、右のCocos2dであるときに検出します

ジェスチャーを使用して移動したいCCSpriteがあります。問題は、私がCocos2Dを完全に初めて使用することです。ジェスチャーがアップのときにSpriteが1つのアクションを実行し、ジェスチャーがダウンしているときに別のアクションを実行します。誰かが私を正しい方向に向けることができますか?

ありがとう!

26
Magnus

明らかに、各UISwipeGestureRecognizerは、指定された方向のスワイプのみを検出できます。方向フラグを一緒にORすることもできますが、UISwipeGestureRecognizerは追加のフラグを無視します。

解決策は、スワイプジェスチャを認識させたい方向ごとに1つのUISwipeGestureRecognizerを追加し、それに応じて各認識機能の方向を上、下、左、右のいずれかに設定することです。任意の方向のスワイプをテストする場合は、4つのUISwipeGestureRecognizerを追加する必要があります。

それはちょっと変ですが、それが私にとってうまくいった唯一の方法です。

58
LearnCocos2D
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)
    {
    }
}
27
DevGom

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.
    }
}
8
Richie Hyatt

デフォルトの方向は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は右にスワイプすると呼び出されます

7
roundsun

各軸(水平および垂直)に1つの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];
1
Damien Romito

ここには良い情報がたくさんありますが、それだけの簡単な答えは見つかりませんでした。

スワイプがleftrightupdownかを区別したい場合は、newUISwipeGestureRecognizer各方向。

しかしながら! 各ジェスチャー認識機能を同じセレクターにルーティングできるため、これはそれほど悪くありません。期待どおりに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];
}

2番目、switchステートメントで方向を区別します。

- (void)userDidSwipeScreen:(UISwipeGestureRecognizer *)swipeGestureRecognizer
{
    switch (swipeGestureRecognizer.direction) {
        case UISwipeGestureRecognizerDirectionLeft: {
            // Handle left
            break;
        }
        case UISwipeGestureRecognizerDirectionRight: {
            // Handle right
            break;
        }
        default: {
            break;
        }
    }
}
0
kgaidis
-(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
 }


}

すべてのifステートメントの代わりにスイッチを使用することもできます

0
Zigglzworth