UISwipeGestureRecognizer 似乎不尊重 iPhone 3G 上当前的 UIDevice 方向
我注册了四个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了转换方向之外没有其他办法。我已经使用此内部方法在 Kobold2D 的滑动手势识别器中完成了此操作,应该很容易适应
:使用Cocos2D 2.x的人应该知道CCDirector不再具有deviceOrientation属性。相反,您只需从 UIDevice 获取方向:
然后只需将 CC 替换为 UI 前缀来更改所有案例标签,例如
应更改为:
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:
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:
Then just change all case labels by replacing the CC with the UI prefix, for example
should be changed to:
我已将 @LearnCocos2D 提供的函数转换为 Cocos2d-iphone 专用代码(即独立于 Kobold2D):
I've converted the function provided by @LearnCocos2D to Cocos2d-iphone only code (i.e. independent of Kobold2D):