xcode 检测 UIGestureRecognizer 何时上、下、左、右 Cocos2d

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7420078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:06:10  来源:igfitidea点击:

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

iphonexcodecocos2d-iphoneuigesturerecognizer

提问by Magnus

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, another one when gesture is down, another action when gesture is right and same thing for left. Can someone point me in the right direction?

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

Thanks!

谢谢!

回答by LearnCocos2D

Apparently each UISwipeGestureRecognizer can only detect the swipe in the given direction. Even though the direction flags could be OR'ed together the UISwipeGestureRecognizer ignores the additional flags.

显然,每个 UISwipeGestureRecognizer 只能检测给定方向的滑动。即使方向标志可以被 OR'ed 一起 UISwipeGestureRecognizer 忽略额外的标志。

The solution is to add one UISwipeGestureRecognizer for each direction you want the swipe gesture to be recognized, and set each recognizer's direction accordingly to either up, down, left and right. If you want to test for a swipe in any direction you'll have to add four UISwipeGestureRecognizers.

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

It's kind of odd but that's the only way it worked for me.

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

回答by DevGom

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

回答by Richie Hyatt

Use a UIPanGestureRecogizer and detect the swipe directions you care about. see the UIPanGestureRecognizer documentation for details. -rrh

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

// 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.
    }
}

回答by roundsun

The defaut direction is UISwipeGestureRecognizerDirectionRight. the multiple directions also can be specified like that :

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

[swipeGesture setDirection: UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionLeft];

/// But if you want to get every single direction ,like that:

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

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

the function handleSwipeGestureLeft will be called when swipe to left,and handleSwipeGestureRight wil be called when you swipe to right

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

回答by Damien Romito

Add one UISwipeGestureRecognizer for each axe (horizontal and vertical):

为每个轴(水平和垂直)添加一个 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];

回答by kgaidis

Even though there's lots of good info here, I couldn't find a quick answer that had it all.

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

If you want to differentiate whether a swipe is leftor rightor upor down, you need to create a newUISwipeGestureRecognizerfor each direction.

如果要区分滑动是left还是rightupdown,则需要为每个方向创建一个UISwipeGestureRecognizer的。

However! This is not so bad, because you can route each of your gesture recognizers to the same selector, that can then use a switch statement as you might expect.

然而!这还不错,因为您可以将每个手势识别器路由到同一个 selector,然后可以像您期望的那样使用 switch 语句。

First, add gesture recognizers for each direction and route them to the same selector:

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

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

Second, differentiate between the directions with a switch statement:

其次,用 switch 语句区分方向:

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

回答by Zigglzworth

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

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