UISwipeGestureRecognizer 似乎不尊重 iPhone 3G 上当前的 UIDevice 方向

发布于 2024-12-23 01:14:38 字数 1661 浏览 1 评论 0原文

我注册了四个 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;

I have four UIGestureSwipeRecognizers registered (one for each direction), and they work as intended on an iPhone 4/4S (iOS 4.3 and 5) and iPad 1/2 (iOS 4.NotSure and 5). It is a game, so the only allowed device orientations are LandscapeRight and LandscapeLeft. However, on an iPhone 3G with iOS 4.1, the swipe recognizers respond as if the device were being held in Portrait. In other words, on the iPhone 3G, what should be an Up swipe in LandscapeLeft gets registered as a Right swipe. In fact, all four swipe recognizers behave as if the device were in Portrait; however, I have checked [[UIDevice currentDevice] orientation] and it always returns UIDeviceOrientationLandscapeLeft

Also, the app is a game built upon the cocos2d 1.0.1 template.

What could I be doing wrong?

Here's my code where I register the four swipe recognizers:

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

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

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

发布评论

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

评论(2

柠栀 2024-12-30 01:14:38

除了转换方向之外没有其他办法。我已经使用此内部方法在 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:

There's no way around it other than converting the directions. I've done this in Kobold2D's swipe gesture recognizer using this internal method, it should be easy to adapt:

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

For those who are using Cocos2D 2.x you should know that CCDirector no longer has the deviceOrientation property. Instead you just get the orientation from UIDevice:

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

Then just change all case labels by replacing the CC with the UI prefix, for example

case CCDeviceOrientationPortraitUpsideDown:

should be changed to:

case UIDeviceOrientationPortraitUpsideDown:
独﹏钓一江月 2024-12-30 01:14:38

我已将 @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;
}

I've converted the function provided by @LearnCocos2D to Cocos2d-iphone only code (i.e. independent of 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文