获取 Cocoa Touch 中的滑动方向
我试图捕捉一个手势,但它不起作用。这是我的代码:
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
它
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
NSLog(@"get gesture");
if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"get gesture right");
}
if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"get gesture Left");
}
}
总是得到一个手势,但无法识别方向。我也尝试过 if(recognizer.direction){NSLog(@"getgesture");} 并且它也有效,所以我不明白我在哪里犯了错误。
感谢您的任何帮助。
I am trying to catch a gesture but it does not work. Here is my code:
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
and
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
NSLog(@"get gesture");
if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"get gesture right");
}
if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"get gesture Left");
}
}
It always gets a gesture but does not recognize the direction. I also tried if(recognizer.direction){NSLog(@"get gesture");}
and it also worked, so I do not understand where I made the mistake.
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您没有正确使用
UISwipeGestureRecognizer
。它的方向始终是您设置的方向(在本例中为 UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft 或 3)。如果您想捕获可以区分的左右滑动,则必须为每个滑动设置一个单独的识别器。 Apple 在他们的 SimpleGestureRecognizers 示例。
You're not using the
UISwipeGestureRecognizer
correctly. Its direction is always going to be what you've set it to (in this caseUISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft
, or 3).If you want to capture swipes left and right that you can differentiate between, you'll have to set up a separate recognizer for each. Apple does this in their SimpleGestureRecognizers sample.
您所要做的只是更改添加手势识别器的代码。
What you have to do is just change the codes for adding gesture recognizer.
UISwipe...是iOS。但对于 Cocoa,您可以在视图类中使用
-swipeWithEvent:
。请参阅以下位置的文档:http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/EventOverview/HandlingTouchEvents/HandlingTouchEvents.html#//apple_ref/doc/uid/10000060i-CH13-SW10
UISwipe... is iOS. But for Cocoa, you can use
-swipeWithEvent:
in your view class. See the documentation at:http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/EventOverview/HandlingTouchEvents/HandlingTouchEvents.html#//apple_ref/doc/uid/10000060i-CH13-SW10
gypsicoder 和 paulbailey 的答案都是正确的。有关我的更详细的解决方案,请参阅:https://stackoverflow.com/a/16810160/936957
Both gypsicoder and paulbailey's answers are right. For a more detailed solution of mine, see: https://stackoverflow.com/a/16810160/936957