问题描述
我试图抓住一个手势,但它不工作。这是我的代码:
UISwipeGestureRecognizer *
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom :)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft)];
[[self view] addGestureRecognizer:recognizer];
[识别器发布];
和
- (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(@get gesture);}
它也工作,所以我不明白我犯了错误。 / p>
感谢您的帮助。
UISwipeGestureRecognizer
。它的方向总是你所设置的(在这种情况下 UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft
,或3)。
如果您要捕获左右滑动,以便区分两者,则必须为每个滑块设置单独的识别器。 Apple在其 SimpleGestureRecognizers示例中执行此操作, a>。
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.
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).
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.
这篇关于获取在Cocoa Touch中的滑动方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!