xcode 在 Cocoa Touch 中获取滑动方向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5322145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get swipe direction in Cocoa Touch
提问by Csabi
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.
它总是得到一个手势,但不识别方向。我也尝试过if(recognizer.direction){NSLog(@"get gesture");}
,它也有效,所以我不明白我在哪里犯了错误。
Thanks for any help.
谢谢你的帮助。
回答by paulbailey
You're not using the UISwipeGestureRecognizer
correctly. Its direction is always going to be what you've set it to (in this case UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft
, or 3).
你没有UISwipeGestureRecognizer
正确使用。它的方向将始终是您为其设置的方向(在本例中为UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft
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.
如果要捕获可以区分的左右滑动,则必须为每个滑动设置单独的识别器。Apple 在他们的SimpleGestureRecognizers 示例中做到了这一点。
回答by gypsicoder
What you have to do is just change the codes for adding gesture recognizer.
您要做的只是更改添加手势识别器的代码。
UISwipeGestureRecognizer *leftRecognizer;
leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[leftRecognizer setDirection: UISwipeGestureRecognizerDirectionLeft];
[[self view] addGestureRecognizer:leftRecognizer];
[leftRecognizer release];
UISwipeGestureRecognizer *rightRecognizer;
rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[rightRecognizer setDirection: UISwipeGestureRecognizerDirectionRight];
[[self view] addGestureRecognizer:rightRecognizer];
[rightRecognizer release];
回答by Yunus Nedim Mehel
Both gypsicoder and paulbailey's answers are right. For a more detailed solution of mine, see: https://stackoverflow.com/a/16810160/936957
gypsicoder 和 paulbailey 的答案都是正确的。有关我的更详细的解决方案,请参阅:https: //stackoverflow.com/a/16810160/936957
回答by Walter
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
UISwipe...是iOS。但是对于 Cocoa,您可以-swipeWithEvent:
在视图类中使用。请参阅以下文档:http:
//developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/EventOverview/HandlingTouchEvents/HandlingTouchEvents.html#//apple_ref/doc/uid/10000060i-CH13-SW10