如何检测滑动手势方向?
我需要检测我的滑动手势的方向,但我遇到了问题。手势有效,但我不知道如何检测方向。 ...
swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(detectSwipe:)];
[swipeGesture setNumberOfTouchesRequired:1];
[swipeGesture setDirection:UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp];
[appView addGestureRecognizer:swipeGesture];
-(void)detectSwipe:(UISwipeGestureRecognizer *)recognizer {
switch (recognizer.direction) {
case UISwipeGestureRecognizerDirectionUp:
NSLog(@"smth1");
break;
case UISwipeGestureRecognizerDirectionDown:
NSLog(@"smth2");
default:
break;
}
}
它不起作用:/
i need to detect direction of my swipe gesture and i've got problem with it. gesture is working, but i don't know how to detect direction.
...
swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(detectSwipe:)];
[swipeGesture setNumberOfTouchesRequired:1];
[swipeGesture setDirection:UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp];
[appView addGestureRecognizer:swipeGesture];
-(void)detectSwipe:(UISwipeGestureRecognizer *)recognizer {
switch (recognizer.direction) {
case UISwipeGestureRecognizerDirectionUp:
NSLog(@"smth1");
break;
case UISwipeGestureRecognizerDirectionDown:
NSLog(@"smth2");
default:
break;
}
}
it's not working :/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是我的一个项目的示例:
Here is an example from one of my projects:
direction
属性仅定义被识别为滑动的允许方向,而不是特定滑动的实际方向。最简单的方法是使用两个单独的手势识别器。您还可以使用
locationInView:
方法在手势开始和结束时检查触摸的位置。The
direction
property only defines the allowed directions that are recognized as swipes, not the actual direction of a particular swipe.The easiest would be to use two separate gesture recognizers instead. You could also inspect the location of the touch when the gesture starts and when it ends with the
locationInView:
method.扩展 omz 的解决方案:
self.myView 是我想要放置手势识别器的视图。下面的代码未经测试,我想最好将识别器保留为属性并将它们添加到 viewDidLoad() 或 xib 中文件。
self
是一个UIViewController
。将这两个方法添加到您的 UIViewController 中并添加必要的操作:
Extending omz's solution:
self.myView
is the view I want to put the gesture recognizer on. The code below is not tested, I guess it would be better to keep the recognizers asproperty
s and add them insideviewDidLoad()
orxib
file.self
is aUIViewController
.Add those two methods to your
UIViewController
and add necessary actions: