如何识别4个方向的滑动?
我需要识别各个方向的滑动(上/下/左/右)。不是同时,但我需要认出它们。
我尝试过:
UISwipeGestureRecognizer *Swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)];
Swipe.direction = (UISwipeGestureRecognizerDirectionLeft |
UISwipeGestureRecognizerDirectionRight |
UISwipeGestureRecognizerDirectionDown |
UISwipeGestureRecognizerDirectionUp);
[self.view addGestureRecognizer:Swipe];
[Swipe release];
但 SwipeRecognizer
上没有出现任何内容
这是 SwipeRecognizer 的代码:
- (void) SwipeRecognizer:(UISwipeGestureRecognizer *)sender {
if ( sender.direction == UISwipeGestureRecognizerDirectionLeft )
NSLog(@" *** SWIPE LEFT ***");
if ( sender.direction == UISwipeGestureRecognizerDirectionRight )
NSLog(@" *** SWIPE RIGHT ***");
if ( sender.direction == UISwipeGestureRecognizerDirectionDown )
NSLog(@" *** SWIPE DOWN ***");
if ( sender.direction == UISwipeGestureRecognizerDirectionUp )
NSLog(@" *** SWIPE UP ***");
}
我该怎么做?如何将所有不同的方向分配给我的 Swipe 对象?
I need to recognize swipes in all directions (Up/Down/Left/Right). Not simultaneously, but I need to recognize them.
I tried:
UISwipeGestureRecognizer *Swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)];
Swipe.direction = (UISwipeGestureRecognizerDirectionLeft |
UISwipeGestureRecognizerDirectionRight |
UISwipeGestureRecognizerDirectionDown |
UISwipeGestureRecognizerDirectionUp);
[self.view addGestureRecognizer:Swipe];
[Swipe release];
but nothing appeared on SwipeRecognizer
Here's the code for SwipeRecognizer:
- (void) SwipeRecognizer:(UISwipeGestureRecognizer *)sender {
if ( sender.direction == UISwipeGestureRecognizerDirectionLeft )
NSLog(@" *** SWIPE LEFT ***");
if ( sender.direction == UISwipeGestureRecognizerDirectionRight )
NSLog(@" *** SWIPE RIGHT ***");
if ( sender.direction == UISwipeGestureRecognizerDirectionDown )
NSLog(@" *** SWIPE DOWN ***");
if ( sender.direction == UISwipeGestureRecognizerDirectionUp )
NSLog(@" *** SWIPE UP ***");
}
How can I do this? How can assign to my Swipe object all different directions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用 UIPanGestureRecogizer 并检测您关心的滑动方向。有关详细信息,请参阅 UIPanGestureRecognizer 文档。 -rr
CHANHE IN STATEEND CODE WIH
//如果您只想从上、下、左、右单击一次
Use a UIPanGestureRecogizer and detect the swipe directions you care about. see the UIPanGestureRecognizer documentation for details. -rrh
CHANHE IN STATEEND CODE WIH THIS
//if YOU WANT ONLY SINGLE SWIPE FROM UP,DOWN,LEFT AND RIGHT
您可以通过 SwipeGesture 只添加一条对角线,然后...
You can add only one diagonal by SwipeGesture, then...
它可能不是最好的解决方案,但您始终可以为要检测的每个滑动方向指定不同的 UISwipeGestureRecognizer。
在 viewDidLoad 方法中,只需定义所需的 UISwipeGestureRecognizer:
然后只需实现相应的方法来处理滑动:
It might not be the best solution but you can always specify different UISwipeGestureRecognizer for each swipe direction you want to detect.
In the viewDidLoad method just define the required UISwipeGestureRecognizer:
Then just implement the respective methods to handle the swipes:
你这样设置方向,
这就是你收到回调时的方向,所以你所有的测试失败都是正常的。如果你有,
测试就会成功(但都会成功,所以你不会从中得到任何信息)。如果您想区分不同方向的滑动,您将需要单独的手势识别器。
编辑
正如评论中指出的,请参阅此答案。显然这也行不通。您应该只创建一个方向的滑动,以使您的生活更轻松。
You set the direction like this
That's what the direction will be when you get the callback, so it is normal that all your tests fails. If you had
The tests would succeed (but the would all succeed so you wouldn't get any information out of them). If you want to distinguish between swipes in different directions you will need separate gesture recognizers.
EDIT
As pointed out in the comments, see this answer. Apparently even this doesn't work. You should create swipe with only one direction to make your life easier.
不幸的是,您不能使用
direction
属性来监听识别器;它只为您提供识别器检测到的方向。为此,我使用了两个不同的UISwipeGestureRecognizer
,请在此处查看我的答案:https://stackoverflow.com /a/16810160/936957Unfortunately you cannot use
direction
property for listening the recognizer; it only gives you the detected directions by the recognizer. I have used two differentUISwipeGestureRecognizer
s for that purpose, see my answer here: https://stackoverflow.com/a/16810160/936957我之前实际上遇到过这个问题。我最终做的是创建一个 UIView 子类,重写
touchesMoved:
并进行一些数学计算来计算方向。总体思路如下:
为了简单起见,在
touchesMoved:
中,我只记录方向,但您可以使用该信息执行您想要的操作(例如,将其传递给委托人、用它发布通知等) .)。在touchesMoved
中,您还需要一些方法来识别滑动是否完成,但这与问题不太相关,所以我将其留给您。calculateDirectionFromTouches
中的数学解释此处。I actually ran into this exact problem before. What I ended up doing was creating a UIView subclass, overriding
touchesMoved:
and doing some math to calculate the direction.Here's the general idea:
For simplicity's sake in
touchesMoved:
, I only log the direction, but you can do what you want with that information (e.g. pass it to a delegate, post a notification with it, etc.). IntouchesMoved
you'll also need some method to recognize if the swipe is finished or not, but that's not quite relevant to the question so I'll leave that to you. The math incalculateDirectionFromTouches
is explained here.我终于找到了最简单的答案,如果您同意,请将其标记为答案。
如果你只有一个方向的滑动+平移,你只需说:
[myPanRecogznier requireGestureRecognizerToFail:mySwipeRecognizer];
但如果您有两次或多次滑动,则无法将数组传递到该方法中。为此,您需要实现 UIGestureRecognizerDelegate。
例如,如果您想识别 2 次滑动(向左和向右)并且还希望允许用户向上平移,则可以将手势识别器定义为属性或实例变量,然后将 VC 设置为平移上的委托手势识别器:
然后实现 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 委托方法,如下所示:
这告诉平移手势识别器仅在左右滑动均无法识别时才工作 - 完美!
希望将来 Apple 能够让我们将数组传递给 requireGestureRecognizerToFail: 方法。
I finally found the simplest answer, please mark this as the answer if you agree.
If you only have one direction swipe + pan, you just say:
[myPanRecogznier requireGestureRecognizerToFail:mySwipeRecognizer];
But if you have two or more swipes, you can't pass an array into that method. For that, there's UIGestureRecognizerDelegate that you need to implement.
For example, if you want to recognize 2 swipes (left and right) and you also want to allow the user to pan up, you define the gesture recognizers as properties or instance variables, and then you set your VC as the delegate on the pan gesture recognizer:
You then implement - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer delegate method, like so:
This tells the pan gesture recognizer to only work if both left and right swipes fail to be recognize - perfect!
Hopefully in the future Apple will just let us pass an array to the requireGestureRecognizerToFail: method.