获取在Cocoa Touch中的滑动方向 [英] Get swipe direction in Cocoa Touch

查看:201
本文介绍了获取在Cocoa Touch中的滑动方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图抓住一个手势,但它不工作。这是我的代码:

  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 UISwipeGestureRecognizercorrectly. 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中的滑动方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆