1

我注册了四个 UIGestureSwipeRecognizers(每个方向一个),它们在 iPhone 4/4S(iOS 4.3 和 5)和 iPad 1/2(iOS 4.NotSure 和 5)上按预期工作。这是一个游戏,所以唯一允许的设备方向是 LandscapeRight 和 LandscapeLeft。但是,在装有 iOS 4.1 的 iPhone 3G 上,滑动识别器的响应就像设备被握在纵向一样。换句话说,在 iPhone 3G 上,LandscapeLeft 中的向上滑动被注册为向右滑动。事实上,所有四个滑动识别器的行为都好像设备处于纵向状态。但是,我已经检查过了[[UIDevice currentDevice] orientation],它总是返回UIDeviceOrientationLandscapeLeft

此外,该应用程序是基于 cocos2d 1.0.1 模板构建的游戏。

我可能做错了什么?

这是我注册四个滑动识别器的代码:

_swipeRecognizer_right = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightDetected)];
_swipeRecognizer_right.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:_swipeRecognizer_right];
_swipeRecognizer_right.delegate = self;

_swipeRecognizer_left = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftDetected)];
_swipeRecognizer_left.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:_swipeRecognizer_left];
_swipeRecognizer_left.delegate = self;

_swipeRecognizer_up = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeUpDetected)];
_swipeRecognizer_up.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:_swipeRecognizer_up];
_swipeRecognizer_up.delegate = self;

_swipeRecognizer_down = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDownDetected)];
_swipeRecognizer_down.direction = UISwipeGestureRecognizerDirectionDown;
[self.view addGestureRecognizer:_swipeRecognizer_down];
_swipeRecognizer_down.delegate = self;
4

2 回答 2

1

除了转换方向之外,没有其他办法。我已经使用这种内部方法在Kobold2D的滑动手势识别器中完成了此操作,它应该很容易适应:

-(KKSwipeGestureDirection) convertSwipeDirection:(UISwipeGestureRecognizerDirection)uiDirection
{
    // portrait mode direction remains unchanged
    KKSwipeGestureDirection direction = (KKSwipeGestureDirection)uiDirection;

    switch (uiDirection)
    {
        case UISwipeGestureRecognizerDirectionRight:
        {
            switch (director.deviceOrientation)
            {
                case CCDeviceOrientationPortraitUpsideDown:
                    direction = KKSwipeGestureDirectionLeft;
                    break;
                case CCDeviceOrientationLandscapeLeft:
                    direction = KKSwipeGestureDirectionUp;
                    break;
                case CCDeviceOrientationLandscapeRight:
                    direction = KKSwipeGestureDirectionDown;
                    break;
                default:
                    break;
            }
            break;
        }

        case UISwipeGestureRecognizerDirectionLeft:
        {
            switch (director.deviceOrientation)
            {
                case CCDeviceOrientationPortraitUpsideDown:
                    direction = KKSwipeGestureDirectionRight;
                    break;
                case CCDeviceOrientationLandscapeLeft:
                    direction = KKSwipeGestureDirectionDown;
                    break;
                case CCDeviceOrientationLandscapeRight:
                    direction = KKSwipeGestureDirectionUp;
                    break;
                default:
                    break;
            }
            break;
        }

        case UISwipeGestureRecognizerDirectionUp:
        {
            switch (director.deviceOrientation)
            {
                case CCDeviceOrientationPortraitUpsideDown:
                    direction = KKSwipeGestureDirectionDown;
                    break;
                case CCDeviceOrientationLandscapeLeft:
                    direction = KKSwipeGestureDirectionLeft;
                    break;
                case CCDeviceOrientationLandscapeRight:
                    direction = KKSwipeGestureDirectionRight;
                    break;
                default:
                    break;
            }
            break;
        }

        case UISwipeGestureRecognizerDirectionDown:
        {
            switch (director.deviceOrientation)
            {
                case CCDeviceOrientationPortraitUpsideDown:
                    direction = KKSwipeGestureDirectionUp;
                    break;
                case CCDeviceOrientationLandscapeLeft:
                    direction = KKSwipeGestureDirectionRight;
                    break;
                case CCDeviceOrientationLandscapeRight:
                    direction = KKSwipeGestureDirectionLeft;
                    break;
                default:
                    break;
            }
            break;
        }
    }

    return direction;
}

对于使用Cocos2D 2.x的用户,您应该知道 CCDirector 不再具有 deviceOrientation 属性。相反,您只需从 UIDevice 获取方向:

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

然后只需通过将CC替换为UI前缀来更改所有案例标签,例如

case CCDeviceOrientationPortraitUpsideDown:

应改为:

case UIDeviceOrientationPortraitUpsideDown:
于 2011-12-26T11:15:59.070 回答
1

我已经将@LearnCocos2D 提供的函数转换为Cocos2d-iphone 专用代码(即独立于Kobold2D):

- (UISwipeGestureRecognizerDirection) convertSwipeDirection:(UISwipeGestureRecognizerDirection)uiDirection
{
    // portrait mode direction remains unchanged
    UISwipeGestureRecognizerDirection direction = (UISwipeGestureRecognizerDirection)uiDirection;

    // Get cocod2d device orientation
    ccDeviceOrientation deviceOrientation = [[CCDirector sharedDirector] deviceOrientation];

    switch (uiDirection)
    {
        case UISwipeGestureRecognizerDirectionRight:
        {
            switch (deviceOrientation)
            {
                case CCDeviceOrientationPortraitUpsideDown:
                    direction = UISwipeGestureRecognizerDirectionLeft;
                    break;
                case CCDeviceOrientationLandscapeLeft:
                    direction = UISwipeGestureRecognizerDirectionUp;
                    break;
                case CCDeviceOrientationLandscapeRight:
                    direction = UISwipeGestureRecognizerDirectionDown;
                    break;
                default:
                    break;
            }
            break;
        }

        case UISwipeGestureRecognizerDirectionLeft:
        {
            switch (deviceOrientation)
            {
                case CCDeviceOrientationPortraitUpsideDown:
                    direction = UISwipeGestureRecognizerDirectionRight;
                    break;
                case CCDeviceOrientationLandscapeLeft:
                    direction = UISwipeGestureRecognizerDirectionDown;
                    break;
                case CCDeviceOrientationLandscapeRight:
                    direction = UISwipeGestureRecognizerDirectionUp;
                    break;
                default:
                    break;
            }
            break;
        }

        case UISwipeGestureRecognizerDirectionUp:
        {
            switch (deviceOrientation)
            {
                case CCDeviceOrientationPortraitUpsideDown:
                    direction = UISwipeGestureRecognizerDirectionDown;
                    break;
                case CCDeviceOrientationLandscapeLeft:
                    direction = UISwipeGestureRecognizerDirectionLeft;
                    break;
                case CCDeviceOrientationLandscapeRight:
                    direction = UISwipeGestureRecognizerDirectionRight;
                    break;
                default:
                    break;
            }
            break;
        }

        case UISwipeGestureRecognizerDirectionDown:
        {
            switch (deviceOrientation)
            {
                case CCDeviceOrientationPortraitUpsideDown:
                    direction = UISwipeGestureRecognizerDirectionUp;
                    break;
                case CCDeviceOrientationLandscapeLeft:
                    direction = UISwipeGestureRecognizerDirectionRight;
                    break;
                case CCDeviceOrientationLandscapeRight:
                    direction = UISwipeGestureRecognizerDirectionLeft;
                    break;
                default:
                    break;
            }
            break;
        }
    }

    return direction;
}
于 2012-01-07T23:37:54.963 回答