Cocos2d 检测 UIGestureRecognizer 何时上下左右
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
显然每个 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.
使用 UIPanGestureRecogizer 并检测您关心的滑动方向。有关详细信息,请参阅 UIPanGestureRecognizer 文档。 -rh
Use a UIPanGestureRecogizer and detect the swipe directions you care about. see the UIPanGestureRecognizer documentation for details. -rrh
默认方向是 UISwipeGestureRecognizerDirectionRight。也可以这样指定多个方向:
///
但是如果你想获得每个方向,就像这样:
向左滑动时将调用函数handleSwipeGestureLeft,向右滑动时将调用handleSwipeGestureRight
The defaut direction is UISwipeGestureRecognizerDirectionRight. the multiple directions also can be specified like that :
///
But if you want to get every single direction ,like that:
the function handleSwipeGestureLeft will be called when swipe to left,and handleSwipeGestureRight wil be called when you swipe to right
为每个斧头(水平和垂直)添加一个 UISwipeGestureRecognizer:
Add one UISwipeGestureRecognizer for each axe (horizontal and vertical):
尽管这里有很多好的信息,但我找不到包含所有信息的快速答案。
如果您想区分滑动是
向左
还是向右
、向上
还是向下
,您需要为每个方向创建一个新UISwipeGestureRecognizer
。但是!这还不错,因为您可以将每个手势识别器路由到同一个选择器,然后可以按照您的预期使用 switch 语句。
首先,为每个方向添加手势识别器,并将它们路由到同一个选择器:
第二,使用 switch 语句区分方向:
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
orright
orup
ordown
, you need to create a newUISwipeGestureRecognizer
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:
Second, differentiate between the directions with a switch statement:
还可以使用 switch 代替所有 if 语句
Can also use a switch instead of all the if statements