Cocos2d 检测 UIGestureRecognizer 何时上下左右

发布于 2024-12-04 11:30:41 字数 136 浏览 3 评论 0原文

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

谢谢!

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?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

套路撩心 2024-12-11 11:30:41

显然每个 UISwipeGestureRecognizer 只能检测给定方向的滑动。即使方向标志可以进行“或”运算,UISwipeGestureRecognizer 也会忽略其他标志。

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

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

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.

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.

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

耳钉梦 2024-12-11 11:30:41
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)
    {
    }
}
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)
    {
    }
}
聊慰 2024-12-11 11:30:41

使用 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.
    }
}

Use a UIPanGestureRecogizer and detect the swipe directions you care about. see the UIPanGestureRecognizer documentation for details. -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.
    }
}
羅雙樹 2024-12-11 11:30:41

默认方向是 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

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

[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

红玫瑰 2024-12-11 11:30:41

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

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

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];
铁轨上的流浪者 2024-12-11 11:30:41

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

如果您想区分滑动是向左还是向右向上还是向下,您需要为每个方向创建一个 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;
        }
    }
}

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 left or right or up or down, you need to create a new UISwipeGestureRecognizer for each direction.

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.

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:

- (void)userDidSwipeScreen:(UISwipeGestureRecognizer *)swipeGestureRecognizer
{
    switch (swipeGestureRecognizer.direction) {
        case UISwipeGestureRecognizerDirectionLeft: {
            // Handle left
            break;
        }
        case UISwipeGestureRecognizerDirectionRight: {
            // Handle right
            break;
        }
        default: {
            break;
        }
    }
}
回忆那么伤 2024-12-11 11:30:41
-(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:)];
    [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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文